Unexpected underflow

时间:2018-04-18 18:19:44

标签: javascript underflow

//Data
var M = 83 * (10 ^ (-5));
var R = 2077;
var Th = 1087;
var Tk = 372;
var Tr = 667;
var a = 90;
var Vclc = 9.135 * (10 ^ (-6));
var Vcle = 7.733 * (10 ^ (-6));
var Vswe = 6.105 * (10 ^ (-5));
var Vswc = 6.294 * (10 ^ (-5));
var Vk = 5.606 * (10 ^ (-6));
var Vr = 19.308 * (10 ^ (-6));
var Vh = 5.606 * (10 ^ (-6));

var ρ = ((2 * Vclc + 2 * Vk + Vswc) / (2 * Tk)) + ((Vr * Math.log(Th / Tk)) / (Th - Tk)) + ((2 * Vcle + 2 * Vh + Vswe) / (2 * Th));
console.log(ρ);
console.log(ρ < Number.MIN_VALUE);

When I run this code in CodePen, I get in the console: -1.4626799636897467 and true, which I think means there is an underflow. However, the value of ρ should be something like k*10^-10, where k is constant around 2000. Since the smallest positive number that can be represented in Javascript is 5e-324, which is way smaller than my value, why do I get an underflow? If it's not an underflow I don't understand how a sum of positive numbers can result in a negative number...

Note: I used information provided here for the check of underflow:How to detect numeric overflow/underflow in JavaScript arithmetic operations? and JavaScript MIN_VALUE Property

1 个答案:

答案 0 :(得分:2)

^是按位异或,而不是取幂。

您可以使用指数表示法输入10的字面幂。xxxEyyxxx * 10^yy

&#13;
&#13;
//Data
var M = 83e-5;
var R = 2077;
var Th = 1087;
var Tk = 372;
var Tr = 667;
var a = 90;
var Vclc = 9.135e-6;
var Vcle = 7.733e-6;
var Vswe = 6.105e-5;
var Vswc = 6.294e-5;
var Vk = 5.606e-6;
var Vr = 19.308e-6;
var Vh = 5.606e-6;

var ρ = ((2 * Vclc + 2 * Vk + Vswc) / (2 * Tk)) + ((Vr * Math.log(Th / Tk)) / (Th - Tk)) + ((2 * Vcle + 2 * Vh + Vswe) / (2 * Th));
console.log(ρ);
console.log(ρ < Number.MIN_VALUE);
&#13;
&#13;
&#13;

您在代码中获得true的原因是因为Number.MIN_VALUE是最小的号,但您的结果是否定的。如果您要测试下溢,则需要检查p的绝对值是否低于Number.MIN_VALUE

但是,由于小于此数字的数字不能用Javascript浮点表示,因此除0以外的任何其他内容的测试实际上都不会为真。