Speed of execution of the functions ceil(),round(),floor() - page 3

 
Nikolai Semko:

Yes, but if:

then it's fine.

It remains to be seen if it is OK for any number x
 

A100:

void OnStart()
{
        Print( (int)(3.0001 + 0.999999999999999)); //все равно 4
        Print( floor(3.00001));                    //3
}




I don't get it. What's the problem?

 
A100:
It remains to be seen if it will be OK for any number x

Of course it won't...

After all, if this happens:

double x=3;
x=x+0.1;
x=x+0.1;
x=x+0.1;
x=x+0.1;
if (x!=3.4) Print("oppps..."); // oppps...

then, the questions are neither for me nor for this idea.

 
Nikolai Semko:
What's the problem?
void OnStart()
{
        double d =  16 + 0.999999999999999;
        Print( (int)(d + 0.999999999999999)); //18
        Print( (int)ceil( d ));               //17
}
 
A100:

see my previous post

 
Nikolai Semko:
then, the questions are neither for me, nor for this idea.
floor(), ceil(), round() are there to avoid questions
 

I think that all these faults above are beyond the scope of practical use of this solution to accelerate rounding of positive integers, because few people need precision at the 16-digit level. And these bugs arise from all sorts of overflows at the dots levels of the compiler itself.

 
A100:
Floor(), ceil(), round() are there for that purpose - so there are no questions

I'm not forbidding you to use them. You are welcome to use them. I will use them myself. But if I create an algorithm where speed is important, I will use this rounding variant taking into account all the nuances of this method. I think it would be useful for other programmers to know about the existence of this alternative. The discussion is exactly what we need to know about the nuances of this rounding method. Thank you all very much for that. Am I not right?

 
Nikolai Semko:

DBL_MIN andDBL_EPSILON don't work - too small. Perhaps it makes sense to leave 0.999999999999999999 (16 nines - the maximum number of digits after the decimal point in double)

So, DBL_EPSILON is 16 decimal places:2.2204460492503131e-016.

In your case, you actually get one, as the difference is only 1e-16, which is 2 times less than epsilon.

 
Alexey Navoykov:

So DBL_EPSILON is 16 decimal places:2.2204460492503131e-016

And in your case you actually get one, as the difference is only 1e-16, which is 2 times less than epsilon.


Yeah, I get that, but it doesn't work. Turns out it doesn't work with 16 nines either (strange, it seemed to work before). It only works with 15 nines.

double x=3;
int Y=(int)ceil(x);
Print(Y);                         // 3
Y=(int)(x+0.999999999999999); 
Print(Y);                         // 3  (15 9-ток)
Y=(int)(x+0.9999999999999999);
Print(Y);                         // 4  (16 9-ток)
Y=(int)(x+1-DBL_EPSILON);
Print(Y);                         // 4
Reason: