Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Round(-0.0066219357357) return -1?

Tags:

delphi

I want to Round(-0.0066219357357) and it gives -1.

Isn't it supposed to be 0? And what can I use to round it correctly?

UPDATE: the number is a result of (LineDirection.X/distance); where LineDirection.X is an integer and Distace is double.

like image 764
Sara S. Avatar asked Dec 02 '22 00:12

Sara S.


1 Answers

Looking into System.pas from XE, all work is done by FISTP instruction:

    procedure _ROUND;
    asm
      ...
            FISTP   qword ptr [ESP]
      ...
    end;

According to the Intel's Instruction Reference, value "is rounded to an integer value, according to the rounding mode specified by the RC field of the FPU control word"

So, I would advise you to check if this RC field of FPU control word is set to the rounding you need.

You can do this by working with whole control word - see Set8087CW, and related xxxx8087CW procedures/functions, with Default8087CW variable.

Or you can try Math.SetRoundMode, as @Uwe Raabe suggested. Your case sounds like either rmUp or rmTruncate rmDown was used.

Theoretically, your CPU can be the reason as well, however, it hardly so.

like image 147
Serhii Kheilyk Avatar answered Dec 15 '22 01:12

Serhii Kheilyk