的FPU的舍入模式只適用於最後二進制位浮點尾數。它不能用於十進制四捨五入。該舍入規則有一個例外:FRNDINT
。該指令根據當前舍入模式(FPU控制字的RC字段的設置),將ST(0)寄存器中的源值舍入爲最接近的整數值,並將結果存儲在ST(0)中。 (Intel Manual)。
對於小數四捨五入,您可以使用FRNDINT
一招:首先計算n * 10^x(x是小數部分所需的小數精度)。然後用FRNDINT
將整數部分捨去並捨去小數部分。最後,您可以將數字除以10^x,或者可以將數字轉換爲字符串並適當插入小數點。
看看這個例子(這是你的代碼,只是一點點修改):
INCLUDE Irvine32.inc
.data
newCtrlWord WORD 010000000000b ; set the RC field to round down toward -∞.
oldCtrlWord WORD ?
sum WORD 25
count WORD 10
stdDev WORD 65
thousand WORD 1000
.code
main:
fstcw oldCtrlWord ; store control word
mov ax, oldCtrlWord
and ah, 11110011b ; clear _only_ RC field
or ah, 00000100b ; set _only_ RC field (rounding to -∞)
mov newCtrlWord, ax
fldcw newCtrlWord ; load control word
fild sum ; load integer into ST(0)
fidiv count ; divide ST(0) by mem int
fimul thousand ; thousand=10^3 - preserve three decimal digits of the fractional part
frndint ; only for this you need the control word
fidiv thousand ; adjust integer to a correct rational number (reverse the `fimul thousand`)
call WriteFloat ; +2.5000000E+000
call CRLF
fild stdDev ; load variance integer into ST(0)
fidiv count ; average the result
fsqrt ; +2.5495097E+000
fimul thousand ; thousand=10^3 - preserve three decimal digits of the fractional part
frndint ; only for this you need the control word
fidiv thousand ; adjust integer to a correct rational number (reverse the `fimul thousand`)
call WriteFloat ; +2.5490000E+000
call CRLF
fldcw oldCtrlWord ; restore control word
exit
END main
「號到了0.001精度朝着」:你的意思是十進制或二進制? – rkhb
我的意思是小數點(浮點數)。 – imparante