mirror of
https://github.com/FunkinCrew/Funkin.git
synced 2024-11-27 01:55:52 -05:00
42 lines
870 B
Haxe
42 lines
870 B
Haxe
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.';
|
|
}
|
|
}
|
|
}
|