gpt4 book ai didi

javascript - 将小数转换为分数时出现意外结果

转载 作者:行者123 更新时间:2023-11-28 02:16:20 28 4
gpt4 key购买 nike

function gcd(a, b) {
return (b) ? gcd(b, a % b) : a;
}
var dec2Frac = function (d) {
var top = d.toString().replace(/\d+[.]/, '');
var bot = Math.pow(10, top.length);
if (d > 1) {
top = +top + Math.floor(d) * bot;
}
var x = gcd(top, bot);
var r1 = top / x;
var r2 = bot / x;
var frac = r1 + "/" + r2;
var parts = frac.split('/');
var simpler = parts[0][0]+'/'+parts[1][0];
return simpler;
};

如果我输入640x960 = 0.66666666666667

我预计结果为 2/3,如下所示:http://www.mindspring.com/~alanh/fracs.html

相反,该函数返回6/1。在这里测试:http://jsbin.com/asoxud/1/

最佳答案

作为 MvG 答案的补充,

我发现这非常有趣,并且想了解 float 是如何存储的以及如何取回 float 的一小部分以便用它们进行计算。

试图自己解决这个问题有点头疼,但当它点击时,我想出了这个Fraction函数,

我不知道这是否对你有帮助,但是

既然已经写好了,为什么不把它留在这里

function Fraction(n, d) {
if ("number" !== typeof n)
throw new TypeError("Excptected Parameter to be of type number");

var strings = n.toString(2).split("."); //Split the number by its decimal point

if (strings.length > 1 && !d) { //No denominator given and n is a float

var floats = [strings[1].substr(0, 27), strings[1].substr(27, 54)]; //Split into to parts

var int64 = [
parseInt(floats[0], 2) << 1,
parseInt(floats[1], 2) << 1
];

var denominator = Math.pow(2, strings[1].length + 1); //
var numerator = int64[0] * Math.pow(2, floats[1].length);

numerator += int64[1];
numerator += parseInt(strings[0], 2) * denominator;

this.numerator = numerator;
this.denominator = denominator;
this.reduce();

this.approx = approx(n);

} else if (strings.length < 2 && !d) { // If no denominator and n is an int
this.numerator = n;
this.denominator = 1;
} else { //if n and d
this.numerator = n;
this.denominator = d;
}

function approx(f, n) {
n = n || 0;
var fraction = new Fraction(1, 1);

var float = Math.pow(f, -1);
var rec = ~~float;
var decimal = float - rec;

if (float.toPrecision(Fraction.precision) == rec)
return new Fraction(1, rec);
var _fraction = approx(decimal, n + 1);

fraction.denominator = rec * _fraction.denominator + _fraction.numerator;
fraction.numerator = _fraction.denominator;

return fraction;

}

}

//The approx precision
Fraction.precision = 10;
Fraction.prototype.toString = function () {
return this.numerator + "/" + this.denominator;
};
Fraction.prototype.gcd = function () {
return (function gcd(u, v) {
return ((u > 0) ? gcd(v % u, u) : v);
})(this.numerator, this.denominator);
};
Fraction.prototype.reduce = function () {
var _gcd = this.gcd();
this.numerator /= _gcd;
this.denominator /= _gcd;
};

Fraction.prototype.valueOf = function () {
return this.numerator / this.denominator;
};




var f = new Fraction(0.3333);
+ f; //0.3333333333
f.toString(); // 6004799502560181/18014398509481984
+ f.approx //0.33333
+ f.approx.toString() //3333/10000

var g = new Fraction(2 / 3);
+ g; //0.6666666666666666
g.toString(); //6004799503160661/9007199254740992
+ g.approx //0.6666666666666666
+ g.approx.toString() //2/3

这是 JSbin还有

关于javascript - 将小数转换为分数时出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16333864/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com