简体   繁体   English

javascript-我的客户端选项是什么?

[英]javascript - What are my client-side options?

I love javascript, don't get me wrong, but my problem is I currently want to develop open source web applications for scientific computations and javascript's arithmetic isn't exactly the most precise. 我喜欢javascript,请不要误会我的意思,但是我的问题是我目前想开发用于科学计算的开源Web应用程序,而javascript的算法并不是最精确的。 I've scripted server-side, but I prefer client-side for the obvious reasons that the experience for the user is generally smoother and there is less load on the server. 我已经在服务器端编写了脚本,但是我更喜欢客户端,这是显而易见的原因,即用户的体验通常更流畅,并且服务器上的负载更少。

What are my options as far as working around this issue? 关于解决此问题,我有哪些选择? I've read somewhere that you can implement languages on top of javascript--would this be worth it and what does this look like? 我读过某个地方,您可以在javascript之上实现语言-这值得吗?它看起来像什么? If I do, say, implement python on top of javascript, does that mean the client needs a python interpreter to use the site? 例如,如果我在javascript上实现python,这是否意味着客户端需要python解释器才能使用该网站?

I just can't handle 我无法应付

0.1 + 0.2 == 0.3 // is False

Floating points operations are approximated calculations 浮点运算是近似计算

This is not wrong see more about it 这没错,请查看更多信息

Weird programing behavior 怪异的编程行为

THis is not specific to javascript but common in programming as a whole 这不是专门针对javascript,而是在整个编程中很常见

Here is what i get on chrome: 这是我在chrome上得到的:

0.1 + 0.2 = 0.30000000000000004;

And here is a simple but excellent read on the subject: 这是关于该主题的简单但出色的阅读:

What Every Programmer Should Know About Floating-Point Arithmetic 每个程序员应该了解的浮点运算法则

Why don't my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004? 为什么我的数字(例如0.1 + 0.2)加起来不是一轮好0.3,而是得到一个像0.30000000000000004这样的怪异结果?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all. 因为在内部,计算机使用的格式(二进制浮点数)根本无法准确表示一个数字,例如0.1、0.2或0.3。

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens. 编译或解释代码时,“ 0.1”已经舍入为该格式的最接近的数字,即使在计算之前,也会产生小的舍入误差。

There are a few libraries that allow for much better mathematical operations: 有一些库允许进行更好的数学运算:

However, I would very strongly suggest doing this server side. 但是,我强烈建议这样做。 You could easily do it via AJAX and not have to worry about responsiveness. 您可以通过AJAX轻松完成此操作 ,而不必担心响应速度。 Javascript just wasn't really built for numbers. Javascript并不是真正为数字构建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM