如何将haxe.Int64转换为Float?

我有类似的东西

var x = haxe.Int64.parseString("1000000000000");


我想将其转换为Float。我查看了the Int64 api docs,找到了fromFloatofInttoInt,但是其中没有toFloat方法。

最佳答案

我检查了Int64Int64Helper,也没有在Haxe标准库中看到该功能。

但是,由MIT许可的thx.core库确实包含一个实现,请参见以下内容:https://github.com/fponticelli/thx.core/blob/master/src/thx/Int64s.hx#L137

using haxe.Int64;

class Int64s {

  static var zero = Int64.make(0, 0);
  static var one = Int64.make(0, 1);
  static var min = Int64.make(0x80000000, 0);

/**
Converts an `Int64` to `Float`;
Implementation by Elliott Stoneham.
*/
  public static function toFloat(i : Int64) : Float {
    var isNegative = false;
    if(i < 0) {
      if(i < min)
        return -9223372036854775808.0; // most -ve value can't be made +ve
      isNegative = true;
      i = -i;
    }
    var multiplier = 1.0,
        ret = 0.0;
    for(_ in 0...64) {
      if(i.and(one) != zero)
        ret += multiplier;
      multiplier *= 2.0;
      i = i.shr(1);
    }
    return (isNegative ? -1 : 1) * ret;
  }
}


使用对我有用的库方法,并在JavaScript目标上进行了如下测试:

import haxe.Int64;
import thx.Int64s;

class Main {
    public static function main():Void {
        var x:Int64 = haxe.Int64.parseString("1000000000000");
        var f:Float = thx.Int64s.toFloat(x);
        trace(f); // Prints 1000000000000 to console (on js target)
    }
}

07-24 21:18