Why does Round (-0.0066219357357) return -1?

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

Shouldn't it be 0 ? And what can I use for proper rounding?

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

+4
source share
3 answers

Looking into System.pas from XE, all the work is done using the FISTP statement:

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

In accordance with the Intel instruction manual, the value "is rounded to an integer value in accordance with the rounding mode specified by the RC field FPU control word"

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

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

Or you can try Math.SetRoundMode as @Uwe Raabe suggested. In your case, it sounds like rmUp or rmTruncate rmDown .

Theoretically, your processor may also be the cause, but this is hardly the case.
+9
source

From Delphi help:

The behavior of a Round can be affected by the Set8087CW or Math.SetRoundMode procedure .

Tested in Delphi 2006, Round and Ceil give the same result: 0.

+6
source

You ask people to decide something for you. Therefore, I can offer the following ideas:

  • save the value of your division in a double variable.

     ... var val : Double; begin val := Expression1/expression2; // use single step in debugger to evaluate and store val := Round(val); // now step over this value. ... 
  • Verification using a literal:

     val := Round(0.006); val := Round(-0.006); 

Observe consistent or inconsistent results, study and publish these results. Remember that there can be many things:

a. What types are involved and what values ​​/ loss of accuracy occur?

Q. Are there any integer overflow errors in your calculations that you did not take into account?

C. Does anyone cut back on any results you didn't take into account?

D. Exceptional circumstances; Someone asked in a comment about another function called Round ()? Cpu problem? Memory corruption think creatively, you are a programmer. Turn on Debug-DCU and one step through the code, in CPU mode. Make sure you are in System.pas _ROUND. Use the debug expression evaluator and the debug window to view variable values. Figure it out.

+3
source

All Articles