问题标签 [floating-point-precision]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
php - 来自 microtime 的圆形浮动
PHP 版本 5.3.6
例子
文件 round.txt 包含以下几行:
131.3048491477966 = 131.3
8.340715885162354 = 8.34
8.198318004608154 = 8.199999999999999
这怎么可能?
c++ - c++ 浮点数问题
我是 C++ 和一般编程的新手。我正在尝试一些小程序,只是为了弄脏我的手。下面是我创建的用于计算圆锥体积的程序。
问题:无论我做什么,输出总是一个整数。我想把它精确到小数点后两位。我尝试将变量更改为 double 和 long double 以获得更高的精度,但没有任何效果。
我怎样才能解决这个问题?为什么会这样?(如果可能,请详细说明)到目前为止,我什至问过几位 UNi 的初级讲师,但没有人给我一个正确的答案。
我在 Fedora 上使用 g++
编辑:我很早就尝试过使用 50 60/70 50 /120 40 这样的输入来给出整数。较小的输入如 3 4 给出小数。
编辑: setprecision() 有效。感谢大家发送这个
ruby-on-rails - 奇怪的 Heroku 浮点位精度错误(ruby on rails)
最初我的模型上的坐标字段使用整数,但是当我尝试部署到 Heroku 时,我被提醒(通过崩溃)我需要它是一个浮点数(因为我的坐标中有小数点)。所以我在我的本地机器上生成了一个 change_column 迁移,以将它们 change_column 改为浮点数。一切顺利。
我尝试再次部署到 heroku,首先使用 a heroku pg:reset
,然后使用heroku db:setup
. 在 db:setup 期间,我收到以下错误:
PGError: ERROR: precision for type float must be less than 54 bits
: CREATE TABLE "landmarks" ("id" serial primary key, "name" character varying(255), "xcoord" float(255), "ycoord" float(255), "created_at" timestamp, "updated_at" timestamp)
所以我生成了另一个 change_column 迁移,这次也使用 :precision 选项(设置为:precision => 50
,小于 54)。我再次经历了整个部署过程,它给了我同样的错误。
难道我做错了什么?在使用 float 之前,我已经将另一个应用程序部署到 Heroku,没有任何修改......
我在本地机器上使用 SQLite,我认为 Heroku 使用 Postgres?
提前致谢!
[编辑:我还应该提到,在我更改:precision
坐标值后显示的错误输出 SQL 仍然显示'float(255)'......不知道为什么]
java - How java stores float v double
I realize a decimal number can only be so precise stored as a float in a binary system but what I don't understand is what is happening at the 7 decimal place in my output of 10/7.
In my first output test I noticed that the 5 (7th place) at the end of the float v the 4 (7th place) followed by a 2 in the double. I would think the 7th place in the float would be a 4 since the 8th place in the double is a 2.
Float 1.4285715 Double 1.4285714285714286
I then ran the next output test using the float format to 17 places. The 8th place in each is different - the float is a 6 which is why the 7th place is rounded to 5 I assume.
Float 1.428571462631225600 Double 1.428571428571428600
In the third output test I tried a direct cast with the string format to see what would happen. I got the same results as the second test.
If I have a simplistic question to a complex floating point storage method - I do apologize.
c# - 这是一个有效的浮点比较,它考虑了一组小数位数吗?
我正在编写一个扩展方法来比较两个浮点数,使用一组小数点(有效数字)来确定它们是否相等,而不是公差或百分比差异。查看有关浮动比较的其他问题,我看到了复杂的实现。我是否过于简单化或者这是否有效?
注意:我正在寻找小数位的比较,而不是公差。我不希望 1,000,000 等于 1,000,001。
sql-server - SQL 空间类型的 WKT 转换中的跨机不一致
在 SQL Server 2008 中转换为空间类型的知名文本 (WKT) 时,我在机器之间遇到了不一致的行为。看起来数据的存储方式相同,但转换回 WKT 的行为因机器而异!
这是我整理的一些内容以查明问题:
在我可用的各种机器上,我看到两个不同的结果:
点(-124.95714099999999 39.326679)
0xe610000000010C1EA5129ED0A94340492A53CC413D5FC0
Microsoft SQL Server 2008 R2(SP1) - 10.50.2500.0 (X64)
6月17日2011 00:54:03 Windows NT 6.1上的
Microsoft Corporation
Developer Edition(64位)(Build 7601 : 服务包 1)
或者
POINT ( -124.957141 39.326679)
0xE6100000010C1EA5129ED0A94340492A53CC413D5FC0
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86)
Apr 2 2010 15:52:06 Windows Service Pack2
版权所有 (c) Microsoft NT6 Corporation
Build Developer Edition。0 )(管理程序)
另一个测试用例显示具有 2008 R2 (RTM) 的 X64 机器给出-124.95714099999999。因此,绝对表明 x86 与 x64。
我至少对缺乏浮点精度有点熟悉,但我不知道它是特定于架构的。似乎使用 SQL 空间存储涉及通过像这样的 WKT 转换的数据。我是否没有看到更合适的策略来处理这些数据?
c - 存储固定范围浮点数的有效方法
我正在处理一个(大)浮点数组,每个浮点数需要 4 个字节。
鉴于我的浮点数介于0 和 255 之间,有没有办法将每个浮点数存储在不到4 个字节中?
我可以对整个数组进行任意数量的计算。
我正在使用 C。
c++ - 为什么浮点变量通过以一种奇怪的方式在点后切割数字来节省价值?
我有这个简单的代码行:
当我打印此 val 或查看范围时,它存储值 123456.13
好的,没关系,它不能将点后的所有数字都存储在 4 个字节中,但为什么它会在点后变成 13?不应该是12吗?
(在win32上使用vc++ 2010 express)
floating-point - 至强 fpu 的最大精度是多少?
“我想我知道”所有 X86/X64 处理器浮点单元的宽度都是 80 位,但我没有找到任何关于此的实质性文档。
任何参考资料?
java - 设置 BigDecimal 的特定精度
我有一个XSD,需要我使用 BigDecimal 来表示纬度/经度。我目前将 lat/lon 作为双精度数,并将它们转换为 BigDecimal,但我只需要使用大约 12 个精度位。我一直无法弄清楚如何设置它。谁能帮我这个?