困惑:Javascript 比较错误

发布于 2024-12-07 18:40:14 字数 1928 浏览 1 评论 0原文

我正在为 Javascript 编写一个绘图/图形库,并遇到了一个似乎是个大问题。

我有 4 个数字输入,用于定义图形将显示的最小值和最大值,就像在 TI-84 图形计算器上一样:XMinXMaxYMin YMax

<div id="window">
    <input type="number" class="bound" id="minX" value="" />
    <input type="number" class="bound" id="maxX" value="" />
    <br />
    <input type="number" class="bound" id="minY" value="" />
    <input type="number" class="bound" id="maxY" value="" />
</div>

然后,我有代码获取这些值,并在修改它们时将它们放入函数 Plot.setBounds(xMin, xMax, yMin, yMax) 中:

var plot1 = new Plot();
$('#window .bound').change(function() {
    var minX = $('#window #minX').val(),
        maxX = $('#window #maxX').val(),
        minY = $('#window #minY').val(),
        maxY = $('#window #maxY').val();
    console.log('modified to:', minX, maxX, minY, maxY);
    plot1.setBounds(minX, maxX, minY, maxY);
    plot1.draw();
});

最后,对于 Plot.setBounds(xMin , xMax, yMin, yMax) 函数,我有:

Plot.prototype.setBounds = function (xMin, xMax, yMin, yMax) {
    if ((xMin < xMax) && (yMin < yMax)) {
        this.bounds = [[xMin, xMax], [yMin, yMax]];
    } else {
        console.error('Plot.setBounds(xMin, xMax, yMin, yMax) requires maximum values that are greater than the minimum values!');
        console.log('received:', xMin, xMax, yMin, yMax);
    }
    return this;
};

但是,当 xMin >= 2 且 xMax >=10 时,会出现错误控制台,因为由于某种原因比较返回 false。您可以检查错误之前和之后给出的数字(修改为已接收),它们将具有相同的值。

另一个奇特的事实是,当手动将相同数字的计算放入控制台时,比较结果会返回 true。这让我相信它与函数的调用有关。

同样有趣的是,只有当 xMin >= 2 且 xMax >= 10 时才会出现该问题。

我有一个预览,您可以在此处进行测试:http://dl.dropbox.com/u/962292/web/plot.js/static.html

我会如果您能为我的情况找到解决方案,我将不胜感激。

I'm writing a plotting/graphing library for Javascript and came across what seems to be a huge issue.

I have 4 number inputs for defining the minimum and maximum values that the graph will display, much like on a TI-84 graphing calculator: XMin, XMax, YMin, YMax.

<div id="window">
    <input type="number" class="bound" id="minX" value="" />
    <input type="number" class="bound" id="maxX" value="" />
    <br />
    <input type="number" class="bound" id="minY" value="" />
    <input type="number" class="bound" id="maxY" value="" />
</div>

Then I have code that grabs these values and puts them into the function Plot.setBounds(xMin, xMax, yMin, yMax) whenever they are modified:

var plot1 = new Plot();
$('#window .bound').change(function() {
    var minX = $('#window #minX').val(),
        maxX = $('#window #maxX').val(),
        minY = $('#window #minY').val(),
        maxY = $('#window #maxY').val();
    console.log('modified to:', minX, maxX, minY, maxY);
    plot1.setBounds(minX, maxX, minY, maxY);
    plot1.draw();
});

Finally, for the Plot.setBounds(xMin, xMax, yMin, yMax) function, I have:

Plot.prototype.setBounds = function (xMin, xMax, yMin, yMax) {
    if ((xMin < xMax) && (yMin < yMax)) {
        this.bounds = [[xMin, xMax], [yMin, yMax]];
    } else {
        console.error('Plot.setBounds(xMin, xMax, yMin, yMax) requires maximum values that are greater than the minimum values!');
        console.log('received:', xMin, xMax, yMin, yMax);
    }
    return this;
};

The problem arises, however, when xMin >= 2 and xMax >=10, where an error is thrown into the console because for some reason the comparison returned false. You can check the numbers given before and after the error (modified to and received), and they will have the same values.

Another peculiar fact is how when the computation for the same numbers is put into the console by hand, the comparison returns true. This makes me believe it has something to do with the calling of the function.

It is also interesting to note how the problem only occurs when xMin >= 2 and xMax >= 10.

I have a preview you can test here: http://dl.dropbox.com/u/962292/web/plot.js/static.html

I'd be infinitely grateful if you could find a solution to my situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-12-14 18:40:14

您有字符串并且需要数字:

var minX = +$('#minX').val(),
    maxX = +$('#maxX').val(),
    minY = +$('#minY').val(),
    maxY = +$('#maxY').val();

+ 是到数字的简单快速转换。并且 ID 是否唯一与 #window 无关。

例如,“9”> “19”; // 正确

You have strings and need numbers:

var minX = +$('#minX').val(),
    maxX = +$('#maxX').val(),
    minY = +$('#minY').val(),
    maxY = +$('#maxY').val();

+ is a simple quick conversion to a number. And IDs are unique to #window is irrelevant.

For example, "9" > "19"; // true

一笔一画续写前缘 2024-12-14 18:40:14

获取数字的另一种方法是使用 parseInt()

Another way to get a number is with parseInt().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文