Created
April 2, 2014 22:23
-
-
Save martinwells/9944457 to your computer and use it in GitHub Desktop.
Convert an int64 to a float and back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haxe.Int64; | |
class Int64Utils | |
{ | |
private inline static var MAX_32_PRECISION = 4294967296; | |
public static function fromFloat(f:Float):Int64 { | |
return Int64.make(Std.int(f/MAX_32_PRECISION), Std.int(f-(f/MAX_32_PRECISION))); | |
} | |
public static function toFloat(i:Int64):Float { | |
return (Int64.getHigh(i) * MAX_32_PRECISION + Int64.getLow(i)); | |
} | |
} |
Sadly this doesn't work:
private inline static var MAX_32_PRECISION:Float = 4294967296.0;
public static function fromFloat(f:Float):Int64
{
var h = Std.int(f / MAX_32_PRECISION);
var l = Std.int(f);
return Int64.make(h, l);
}
trace(fromFloat(Std.parseFloat("5000000000"))); // yields 6442450944 on neko
See HaxeFoundation/haxe#3655 for a fromFloat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've corrected code as following: