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

 
Nikolai Semko:


Of course you're right. But I'll repeat. For 99.9% of tasks it is absolutely correct to replace rounding functions with alternative faster variant using conversion of fractional types to (int) or (long). An experienced programmer should be aware of this fact and apply it when it is expedient.

In 99% of cases there is no need to bother with rounding at all. For price comparisons I have this nonsense:

// 0 - first is greater than second
// 1 - equal
// 2 - second is greater than first
uchar compare_prices(double first, double second)
{
   if(first - second >= _Point)
      return 0;
   if(second - first >= _Point)
      return 2;

   first = round(first/_Point);
   second = round(second/_Point);
   
   if(first > second)
      return 0;
   if(first < second)
      return 2;
   return 1;
}

You may notice that in most cases it won't come to round() at all, although I haven't measured the speed.

 
Nikolai Semko:

see above

when x = 3 (any integer) there is a jam :))

Is it mine or your formula that has a joint?

Respectfully.
 
Andrey Kisselyov:
Is there a bug in mine or in your formula?

Respectfully.

In my old one:

y=ceil(x);  -> y=(int)(x+1);

and in yours:

y=ceil(x);  -> y=(int)(x)+1;

when x=3 in both cases will be the wrong value of 4

and the variant:

y=ceil(x);  -> y=(int)(x+0.9999999999999997);

is so far the most correct while maintaining maximum speed of execution.

 
Nikolai Semko:

In my old one:

and in yours:

at x=3 in both cases there will be an incorrect value of 4

and in case of variant:

is so far the most correct while maintaining maximum speed of execution.

but why do you need this formula when you give an integer to its input?

with respect.

P.S. you need to understand, if you want to be a good programmer, that not everywhere and not always you need to shove all sorts of accelerating functions, it depends on the task at hand and the algorithm you're working with.
 
Andrey Kisselyov:

Why do you need this formula when you give an integer to its input?

Sincerely.


You may not know whether it is an integer or a non-integer. There are times when a double number can become an integer.

 
Andrey Kisselyov:

P.S. You have to understand, if you want to be a good programmer, that not everywhere and not always you need to shove all sorts of accelerating functions, it depends on the task at hand and the algorithm you are working with.

Of course. That's why I wrote "apply when appropriate".
 
Nikolai Semko:

You may not know whether it is an integer or a non-integer. There are times when a number can become an integer.

Then go straight for your variants of input numbers.
if(x-(int)(x)>0)y=(int)(x)+1;
else y=x;

Respectfully.

P.S. double by definition cannot be an integer, the representation of a number in machine memory will not change.
 
Andrey Kisselyov:
Then you have to make a big deal about your choices of numbers on the input.

respectfully.


that's why, so we don't have to make a big deal out of it:

if(x-(int)(x)>0)y=(int)(x)+1;
else y=x;

It's easier to write:

y=(int)(x+0.9999999999999997);

or this.

#define _ceil(x)  (int)((x)+0.9999999999999997)
...
y=_ceil(x);
 
Nikolai Semko:

so you don't have to make a big deal out of it:

it's easier to write:

at
1-0.9999999999999998

you won't get it right.
With this approach, you can find a number at which your formula will not work.

Respectfully.

 
Andrey Kisselyov:
at

It's not going to work.
With this approach, you'll find a number at which your formula won't work.

Respectfully.


I've already written about it here.

I suggest you reread this thread so you don't repeat yourself.