2014-04-29 39 views
2

我正在嘗試將所有浮點數編寫爲.001精度朝-∞。我已經將RC字段設置爲01二進制,但我只能打印出想要的精度的初始浮點數,然後忽略四捨五入。我想我可能會錯過某些明顯的方法來處理-∞的精度,但我不確定。程序集 - 圓形浮點數爲.001精度朝向-∞

INCLUDE Irvine32.inc 

.data 

ctrlWord WORD 010000000000b ; set the RC field to round down toward -∞. 

.code 

    fild sum       ; load integer into ST(0) 
    fidiv count      ; divide ST(0) by mem int 
    fstcw ctrlWord     ; store control word 
    fldcw ctrlWord     ; load control word 
    call WriteFloat     ; +2.5000000E+000 

    fild stdDev      ; load variance integer into ST(0) 
    fidiv count      ; average the result 
    fsqrt     ; ST(0) = square root 
    call WriteFloat     ; +2.5495097E+000 
+0

「號到了0.001精度朝着」:你的意思是十進制或二進制? – rkhb

+0

我的意思是小數點(浮點數)。 – imparante

回答

1

的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