groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <>
Subject RE: Math Operations
Date Fri, 09 Oct 2015 18:55:37 GMT
OK, so int + BigDecimal is a bug in documentation then.

I am using Groovy Shell (2.4.3, JVM: 1.8.0_45), and in that I run this:

groovy:000> println (((byte)1 + (short)1).class)
class java.lang.Byte
===> null
groovy:000> println ((byte)1 + (short)1)
===> null

I wondered if Groovy was somehow picking the "smallest" type, because byte + short -> byte
makes no sense at all unless Groovy was looking at the result. However, this doesn't look
good at all:
groovy:000> println ((byte)1 + (short)700)

It looks like it really is doing the math as bytes. What version of Groovy are you using where
it works?


-----Original Message-----
From: Jochen Theodorou [] 
Sent: Friday, October 09, 2015 2:46 PM
Subject: Re: Math Operations

Am 09.10.2015 um 20:23 schrieb Winnebeck, Jason:
> I've been struggling with types in math operations in Groovy. I think 
> there is an error in the documentation?
> It says that int and BigDecimal result in double, but it doesn't, it 
> results in BigDecimal instead. It appears this holds for byte, char, 
> short, and long as well.
> assert (15.0 + 1).class == BigDecimal

integral type with BigDecimal should lead to BigDecimal. So yes, that's a bug.

> Are there other errors in that table? It seems so... because byte + short
> is a byte and not an int as mentioned in that table:
> assert ((byte)1 + (short)1).class == Byte

that assert fails for me. I get int (well Integer).

Anyway.... the BigDecimal part in that table looks wrong... BigDecimal 
leads always to BigDecimal, except for float/double.

bye blackdrag

Jochen "blackdrag" Theodorou

This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.

View raw message