Dechenjm / crack-language

Automatically exported from code.google.com/p/crack-language
Other
0 stars 0 forks source link

Float conversion mixup #56

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In version ae618271abe3, run the following:

float PI = 3.14159265358979323846; 
float val=-2.0*PI;
uint i=1;
val=i*PI;

This results in:
ParseError: valtest.crk:4: Assigning variable val of type float32 from value of 
type float64

The message implies that 'val' is a float32, but it isn't.
The message should read ' Assigning variable val of type float64 from value of 
type float32'

Secondly, I think the right-hand expression is upcasted to a float32 instead of 
a float64.
For my machine float == float64

Original issue reported on code.google.com by Conrad.S...@gmail.com on 29 Oct 2010 at 11:31

GoogleCodeExporter commented 9 years ago
Actually, the error message is correct - "float" is currently aliased based on 
sizeof(int) in the C++ compiler.  For most 64 bit compilers, sizeof(int) is 
still, paradoxically, 32 bit.  We may want to reconsider this and alias float 
based on sizeof(float) in the compiler, though that's also 32 bit on the only 
64 bit compiler I have handy right now.

The reason you're getting that error is because uint is aliased to uint32 and 
uint32 won't implicitly convert to float32 because doing so can result in 
precision loss.  But it can convert to float64, so the expression "i*PI" ends 
up using "float64 * float64", which of course returns float64.  

This is certainly unintuitive, though, and it adds weight to the argument for 
relaxing the restrictions on implicit conversions.

the expression "val=float(i)*PI" will work.

Original comment by mind...@gmail.com on 30 Oct 2010 at 3:56

GoogleCodeExporter commented 9 years ago
Per discussion on the mailing list, we'll deal with this in Issue 9 when we 
create real int/uint/float types

Original comment by mind...@gmail.com on 2 Nov 2010 at 2:41