Question

i have i number 1.010101101 and the type of the number is REAL8 and i try to do default rounding method "Round to nearest even", i saw a lot of example and its all about rounding up and the use frndint but after a lot of serch i realized it's not for default rounding i am right or wrong ?

if it's wrong can you please explain to me how to do it using MASM assembly ??

this is my code:

 .686
 .model flat,stdcall
 .stack 4096

 include irvine32.inc
 include macros.inc
 include floatio.inc

 .data
 R REAL8 1.010101101

  .code
  main proc

   finit
   call ShowFPUStack
   fld R
   call ShowFPUStack
   frndint
   call ShowFPUStack

 exit
 main endp
 end main

and this is my FPU Stack

------ FPU Stack ------

------ FPU Stack ------
ST(0): +1.0101011E+000

------ FPU Stack ------
ST(0): +1.0000000E+000
Was it helpful?

Solution

The x87 FPU implements four rounding methods in hardware. Those are :

  • Round to nearest (even)
  • Round down, towards negative infinity
  • Round up, towards positive infinity
  • Round toward zero

The "Round to nearest (even)" method is used by default by the FPU, so there's a high chance you're already using it. The rounding mode is taken into account when the FPU is making calculations, and conversions between different floating-point formats (32, 64, and 80 bits in this case).

The rounding mode is controlled by the x87 Control Word. You can set the new value with the FLDCW instruction, or get the current value via FSTCW for verification. For more information about the instructions and the format of the word, please see 8.1.5 of the Intel Basic Architecture manual.

However, please do note that the control word is supposed to be controlled only by operating system software, not by applications themselves. If you choose to change the control word, make sure you restore it before quitting your program.

FRNDINT is used to round a floating-point value currently on top of the stack to the nearest integral value. With rounding to even, the result of rounding 1.010101101 will be 1. However, if you turn on rounding towards positive infinity, then the result will be 2.

OTHER TIPS

As Daniel Kamil Kozar said:

The "Round to nearest (even)" method is used by default by the FPU, so there's a high chance you're already using it. The rounding mode is taken into account when the FPU is making calculations, and conversions between different floating-point formats (32, 64, and 80 bits in this case).

so this easy way to implant Round to nearest (even)

.686
.model flat,stdcall
.stack 4096

include irvine32.inc
include macros.inc
include floatio.inc

.data
R REAL8 1.010101101b
l REAL4 0.0



.code
main proc
finit
call ShowFPUStack
fld R
call ShowFPUStack
fst l
fld l
call ShowFPUStack



exit
main endp
end main
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top