Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer Arithmetics #4

Open
JonasKunze opened this issue Aug 20, 2014 · 0 comments
Open

Integer Arithmetics #4

JonasKunze opened this issue Aug 20, 2014 · 0 comments

Comments

@JonasKunze
Copy link
Member

Currently there are some lines with similar code to the following in StrawL1Algorithm.cpp:

int j=(floor) (i/2);
(int) floor((i%8)/2.0);

The usage of floor in these cases does not change the result but only takes CPU time. To understand this you need to know some basics in integer arithmetics. In a few words this is:

  • If a, b are integers, a/b is the integer with the largest absolute value which is smaller or equal to (a/(float)b): 3/2==1, -3/2==-1
  • If f is a double or float, floor(f) is the largest integer (I'm not talking about the c type int) smaller or equal to f, stored in a double or float: floor(1.5)==1, floor(-1.5)==-2

So this means that i/2 is already an integer equal to (int)floor(i/2) because floor(j)==j for every j of type int.
Additionally i/2==(int)floor(i/2.0) if i>=0 (please note the 2.0 is now a double so i/2.0 is also a double). So using floor here is useless and only consumes about a factor of 10 times more CPU time:

int j = i/2; // faster version of int j=(floor) (i/2);
(i%8)/2; // faster version of (int) floor((i%8)/2.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant