1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-14 02:42:55 +00:00
Funkin/source/funkin/util/tools/Int64Tools.hx

43 lines
870 B
Haxe
Raw Normal View History

package funkin.util.tools;
import haxe.Int64;
/**
* Why `haxe.Int64` doesn't have a built-in `toFloat` function is beyond me.
*/
class Int64Tools
{
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);
}
public static function toFloat(i:Int64):Float
{
var f:Float = Int64.getLow(i);
if (f < 0) f += MAX_32_PRECISION;
return (Int64.getHigh(i) * MAX_32_PRECISION + f);
}
public static function isToIntSafe(i:Int64):Bool
{
return i.high != i.low >> 31;
}
public static function toIntSafe(i:Int64):Int
{
try
{
return Int64.toInt(i);
}
catch (e:Dynamic)
{
throw 'Could not represent value "${Int64.toStr(i)}" as an integer.';
}
}
}