- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,该程序在循环中使用 Nilakantha 系列计算 Pi,精度至少为 0.05%。这个循环的退出条件应该是当前计算的值 res 和之前计算的值 prev 匹配时 |res - prev| <= 0.0005。我已经阅读了 FASM 中的一些浮点比较,但仍然不完全理解它是如何工作的。目前程序只是无限执行,永远不会退出循环。在调试过程中,我看到浮点数经常变成 1.#IND00,它应该是一个 NaN。如何写出准确的比较?
format PE console
entry start
include 'win32a.inc'
section '.code' code readable executable
; 3 + 4/(2*3*4) - 4 / (4*5*6) + 4/(6*7*8) - ...
start:
FINIT
piLoop:
; calculating denominator of fraction that will be added: x1*x2*x3
FLD [denominator]
FMUL [zero]
FADD [x1]
FMUL [x2]
FMUL [x3]
FSTP [denominator]
; changing denominator product values for next loop: x1 +=2, x2 += 2, x3 += 2
FLD [x1]
FADD [stepValue]
FSTP [x1]
FLD [x2]
FADD [stepValue]
FSTP [x2]
FLD [x3]
FADD [stepValue]
FSTP [x3]
;calculating numerator: multiplying numerator by -1
FLD [numerator]
FMUL [sign]
FSTP [numerator]
; calculating fraction: +-4 / (x1 * x2 * x3)
FLD [numerator]
FDIV [denominator]
FSTP [fraction]
; adding calculated fraction to our answer
FLD [res]
FADD [fraction]
FSTP [res]
; the comparison part, incorrect?
FLD [res]
FSUB [prev]
FABS
FCOM [accuracy]
FSTSW AX
SAHF
add [i], 1
; prev = res
FLD [res]
FSTP [prev]
jb endMet
jmp piLoop
endMet:
invoke printf, steps_string, [i]
invoke getch
invoke ExitProcess, 0
section '.data' data readable writable
steps_string db "Calculation completed. The Nilakantha Series took %d steps.",10,0
pi_string db "accurate pi = %lf, calculated pi = %lf", 10, 0
res dq 3.0
x1 dq 2.0
x2 dq 3.0
x3 dq 4.0
stepValue dq 2.0
fraction dq 0.0
numerator dq -4.0
denominator dq 0.0
sign dq -1.0
zero dq 0.0
N dd 20
i dd 0
accuracy dq 0.0005
calc dq ?
prev dq 3.0
section '.idata' import data readable
library kernel, 'kernel32.dll',\
msvcrt, 'msvcrt.dll',\
user32,'USER32.DLL'
include 'api\user32.inc'
include 'api\kernel32.inc'
import kernel,\
ExitProcess, 'ExitProcess',\
HeapCreate,'HeapCreate',\
HeapAlloc,'HeapAlloc'
include 'api\kernel32.inc'
import msvcrt,\
printf, 'printf',\
sprintf, 'sprintf',\
scanf, 'scanf',\
getch, '_getch'
最佳答案
(只是扩展我的评论,以便得到答案。)
背景:浮点比较的复杂指令序列来自于早期 x86 CPU 没有板载 FPU 的事实;它是一个可选的独立芯片,它与 CPU 交互的能力是有限的。所以 FCOM 指令不能直接设置 CPU 的 FLAGS 寄存器。相反,它设置浮点状态字,这是浮点协处理器内部的。 FSTSW 指令可用于从协处理器获取状态字并将其加载到通用 CPU 寄存器中,然后 SAHF 将获取 AH 的相应位并将它们写入 FLAGS。
完成所有这些之后,您最终获得了 FLAGS 设置以指示比较的结果,并且状态字的位被布局以便以与整数比较相同的方式设置 FLAGS:如果数字是相等,如果差值严格为负,则为 CF,依此类推。所以你现在可以使用条件跳转,比如 ja
, jb
等,就像无符号整数比较一样。请注意,PF=1 表示比较是无序的(至少一个操作数是 NaN),因此您需要先检查一下。
(PPro 添加了 FCOMI,它从 FP 比较设置 EFLAGS 的方式与 fcom/fstsw/sahf 相同,避免了额外的指令。另见 Why do x86 FP compares set CF like unsigned integers, instead of using signed conditions? )
然而,您的代码有 add [i], 1
中间 ,和大多数 x86 算术指令一样,它根据结果设置 FLAGS。所以您仔细检索的 FLAGS 被覆盖 ,以及 jb
几行是基于 add
的结果而不是 FCOM
.因此你需要重新排列它们。
例如,做 add
之前 SAHF
.或之前 fcomi
.
关于assembly - 在给定精度下比较 FASM 中的 80 位 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64495829/
关于这个话题已经说了很多,但是我找不到我的问题的确切答案。 JavaScript 无法准确表示 0.1 等小数,这是可以理解的。 例如,由于乘法运算期间发生舍入误差,这是正确的: 0.1 * 3 ==
在 zig 中,可以使用“{d}”以十进制表示法打印浮点值。这将自动以全精度打印该值。有没有办法指定位数?是针对每个值,还是作为某种全局设置? 最佳答案 这将限制小数点后的位数,四舍五入和零填充: f
我正在进行的项目需要高精度。减法时我遇到的问题在这里说明: >> 1-0.9999999999999999 ans = 1.1102e-16 >> 1-0.99999999999999999 ans
是否可以使变量本身的精度成为将在运行时定义的变量? 说,如果我尝试编译: SUBROUTINE FOO( VARIABLE, PRECISION_VALUE ) IMPLICI
我正在查询 SQLite 数据库以获取纬度/经度详细信息。 SELECT * FROM tblMain where latitude > -33.866 and latitude 151.20
我一直使用下划线将整数定义为 Fortran 中的特定类型。 下面是一段代码,用于演示 1_8 的含义,例如: program main implicit none integer(2)
我正在寻找一种方法来告诉 pint 要打印多少个有效数字。例如,当我输入以下内容时: import pint ureg = pint.UnitRegistry() print(3*ureg.m /9)
我正在从事一个项目,目标是从山上追踪动物。在第一个实地考察季中,我们使用了 OpenTags 和经过校准的摄像头,虽然可以正常工作,但需要大量的处理/校准,而且至关重要的是,当系统出现问题时无法提供任
在 JavaScript 中有没有一种方法可以确定一个数除以另一个数是否会得到整数?就像 18.4/0.002 给我们 9200,但是 18.4/0.1 给我们 183.99999999999997。
我正在尝试使用 Big.js 在 javascript 中完成此计算 r = (a * b)/ sqrt( ( a*sin(θ) )^2 + ( b*cos(θ) )^2 ) 我也试过 math.js
我有这个片段着色器代码,它在 iOS 模拟器(非视网膜)和 iPad2(非视网膜)之间显示不同: highp vec2 textCoord; textCoord.x = gl_Fr
这个问题在这里已经有了答案: C++ calculating more precise than double or long double (2 个答案) 关闭 6 年前。 是否有任何浮点类型在小
我似乎一直困惑的三个问题: 为什么代码是 x & ~077比这行代码 x & 0177700 更好。是因为精度损失较小吗? 为什么此代码对于设置数字中的第 5 位不正确? num = num + 0x
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Precision of Floating Point 我正在尝试使用一些 float 来计算概率,但我的最
由于微 Controller 的精度,我定义了一个包含两个 float 比率的符号,而不是直接写结果。 #define INTERVAL (0.01F/0.499F) 代替 #defi
我试图比较这 3 种搜索算法,起初我使用 time.h 库但没有任何反应,输出始终是 0.00000 秒。现在我试图在循环中使用一些计数器。但我在这里也有问题, 任何人都可以帮我处理代码吗? 这是我的
char buf[10]; int counter, x = 0; snprintf (buf, sizeof buf , "%.100d%n", x, &counter); printf("Coun
我注意到在评估向量时对我来说是不可预测的行为。直接执行它与在循环中进行索引似乎是完全不同的。谁能帮我解决这个问题?我知道可能在它如何进行每个操作中都有解释,所以我需要一些关于如何查找它的键 多谢指教提
我想在我的应用程序中使用精确的 gps 定位。所以我遵循了一个简单的教程(LocationManager 的基本用法,明确要求 GPS 提供商,要求更新 0 ms,0 m)并创建了一个应用程序。我对更
float 在 1.0f 和 0.0f 之间有多少位精度,这样每个值都可以唯一表示? 例如,如果第一个小数 float 不能表示 0.13f,答案就是 float 只有一位精度。 最佳答案 std::
我是一名优秀的程序员,十分优秀!