How one can do floating point operations with using only integer operations e.g. finding r in x^2 + y^2 = r^2 with out using any floating point operations
Thanks
This is a discussion on doing floating operations using integer operations within the C Programming forums, part of the General Programming Boards category; How one can do floating point operations with using only integer operations e.g. finding r in x^2 + y^2 = ...
How one can do floating point operations with using only integer operations e.g. finding r in x^2 + y^2 = r^2 with out using any floating point operations
Thanks
x*x + y*y? Don't think there's any different approach to sqrt() though.
In situations where you need multiplication you can use fractions instead of floats, e.g (x*355)/113 instead of x*pi or (x*95)/100 instead of x*0.95. This introduces a possible problem with integer overflow though.
Why don't you want to use floating point numbers?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
You could use fixed-point math. But you'll have to write pretty much everything yourself.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Floating point on modern machines is a tad faster than integer ops. No need for fixed point anymore.
How one can do floating point operations ... ... ... with out using any floating point operations
unsigned long long comes to mind.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
?Originally Posted by jafet
how does a 64 bit integer solve his problem?
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
You use fixed point math, which, as Bubba pointed out, isn't much faster anymore.
An example is storing monetary values in an integer, the cent value in the first two digits, and dollars in the other digits. To store $14.68, you would store 1468. There's a fixed point -- the decimal point is always in the second place.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Sometimes you need accuracy over speed. Floating point isn't accurate.
Quzah.
Hope is the first step on the road to disappointment.
Like with money. You wouldn't want ten thousand 99 dollar items to cost $9900.13, now would you?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Of course I would! That'd be nearly 99% off the store price! It's always nice to buy in bulk Unless you meant, of course, 99 cents per item.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}