Int64 to Float?

1,172 views
Skip to first unread message

Andy Li

unread,
Apr 30, 2014, 12:07:21 AM4/30/14
to haxe...@googlegroups.com
Hi,

It seems to me strange that Int64 class does not have a toFloat method.
So how should I convert a Int64 number to Float, considering it could be outside the 32-bit range?

Best regards,
Andy

Justin L Mills

unread,
Apr 30, 2014, 5:50:06 AM4/30/14
to haxe...@googlegroups.com
Andy

We should add a Float64 to Haxe.

Best Justin


Andy Li:

Andy Li

unread,
Apr 30, 2014, 6:02:55 AM4/30/14
to haxe...@googlegroups.com
The reason I mentioned the value of Int64 could be outside the 32-bit range, is that we cannot use toInt:
    var float:Float = Int64.toInt(int64); //toInt will fail, because Int is 32-bit only

Float is a floating point number which is already able to store the whole range of Int64, no matter using single/double precision.
And actually all the current Haxe targets use double precision floating point number for Float, which is 64-bit.

Thanks for your reply though :)

Best regards,
Andy

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Nicolas Cannasse

unread,
Apr 30, 2014, 6:08:59 AM4/30/14
to haxe...@googlegroups.com
Le 30/04/2014 12:02, Andy Li a écrit :
> The reason I mentioned the value of Int64 could be outside the 32-bit
> range, is that we cannot use toInt:
> var float:Float = Int64.toInt(int64); //toInt will fail, because
> Int is 32-bit only
>
> Float is a floating point number which is already able to store the
> whole range of Int64, no matter using single/double precision.
> And actually all the current Haxe targets use double precision floating
> point number for Float, which is 64-bit.

That's not true, since even in double precision you will only get 52
bits of fraction, so you will lose some lower bits precision compared to
Int64.

Best,
Nicolas

Nicolas Cannasse

unread,
Apr 30, 2014, 6:10:25 AM4/30/14
to haxe...@googlegroups.com
Le 30/04/2014 12:02, Andy Li a écrit :
> The reason I mentioned the value of Int64 could be outside the 32-bit
> range, is that we cannot use toInt:
> var float:Float = Int64.toInt(int64); //toInt will fail, because
> Int is 32-bit only
>
> Float is a floating point number which is already able to store the
> whole range of Int64, no matter using single/double precision.
> And actually all the current Haxe targets use double precision floating
> point number for Float, which is 64-bit.

For the same reason there can be lose of precisions when multiplying two
Int32 stored as double precision floats.

Best,
Nicolas

Andy Li

unread,
Apr 30, 2014, 6:14:03 AM4/30/14
to haxe...@googlegroups.com
Thanks for the explanation :)
If I allow lose of precision, what is the best way to convert Int64 to Float?

Best regards,
Andy


Nicolas Cannasse

unread,
Apr 30, 2014, 8:05:56 AM4/30/14
to haxe...@googlegroups.com
Le 30/04/2014 12:14, Andy Li a écrit :
> Thanks for the explanation :)
> If I allow lose of precision, what is the best way to convert Int64 to
> Float?

lowInt32 + highInt32 * 2^32 ?

Best,
Nicolas

David Elahee

unread,
Apr 30, 2014, 8:08:17 AM4/30/14
to haxe...@googlegroups.com
There is no perfect answer, i propose some not so crappy one ...

var a :Float =  0 
a += int64.low;
a += float(int64.high) * float(1<<32) 

You have to use less multiply and use more power of 2 floats  as possible me think...
--
David Elahee


David Elahee

unread,
Apr 30, 2014, 8:08:37 AM4/30/14
to haxe...@googlegroups.com
pwned by Nicolas...
--
David Elahee


Reply all
Reply to author
Forward
0 new messages