user2203774
user2203774

Reputation: 619

How to round a floating-point to the nearest .001

Can someone give me some tips on how to round a floating-point to the nearest .001 in Assembly (MASM)? I have 2 integers. I need to show the quotient as a floating-point number (num_1 / num_2). I got the floating point arithmetic part working, but I couldn't figure out how to round the result. Any suggestion is greatly appreciated!

;calculate the quotient as a floating-point number
    fild    num_1
    fidiv   num_2
    roundps

    ;display the quotient as a floating-point number        
    call    WriteFloat
    call    CrLf

Upvotes: 1

Views: 2130

Answers (2)

Brendan
Brendan

Reputation: 37232

Multiply by 1000 (fmul), round to integer (frndint), then divide by 1000 (fdiv).

The roundps instruction requires SSE4.1 and only works with SSE or AVX registers, not the FPU's registers.

Upvotes: 1

Jens Björnhager
Jens Björnhager

Reputation: 5649

You can scale the number by 1000, round it and then scale back.

Upvotes: 1

Related Questions