![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 68
Rep Power: 3
![]() |
How to get more precise floating-point arithmetic?
How do I make a floating point arithmetic more precise?
Let me show the code first, it probably can explain my question faster than in words. int main()
{
double n = 0.0;
while (n < 2.5e-9)
{
// perform some operation
n += 5.0e-9;
}
}0 5.0000000000000001e-009 1.0000000000000000e-008 1.5000000000000002e-008 2.0000000000000000e-008 2.4999999999999999e-008 (woah ! because of this, I get an extra iteration) 2.9999999999999997e-008 Now, basically I am trying to increment a float variable by 5e-9 on each loop, but it seems like the addition is not precise. I get an extra iteration because of this "imperfection". And oh, I need to use floating point instead of integer in the while-loop statement, so "using integer instead" cannot be the solution. And the 2.5e-9 is actually a variable, therefore in different cases, it's a different value, thus doing something like: while (n < Var - 0.000000001) doesn't help too, because in some cases, I might lose an iteration instead. Anyway to prevent the extra iteration? Thanks in advance. |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Re: How to get more precise floating-point arithmetic?
Floating points are inherently inaccurate, so using build-in comparison operators doesn't work as well as you might expect.. You might see if your compiler has a
long double type (not all compilers necessarily support this type; if they don't it will be equivalent to double), but otherwise the best bet would probably be to write comparison helpers that take an epsilon into account (let epsilon be a very small number).
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
Re: How to get more precise floating-point arithmetic?
You will want to keep an integer counter, i, and calculate the double n by multiplying i by 5.0e-9.
int i = 0;
double n = 0.0;
while(n < 2.5e-9) {
n = (++i)*5.0e-9;
}Or else you'll get an avalanche effect occuring on the decimal values. And then you can rewrite the while condition in terms of i, instead of n. Also, using an epsilon smaller than your increment is a good idea (see Jimbo's post). |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Floating point representation | titaniumdecoy | Coder's Corner Lounge | 6 | Jan 29th, 2008 10:21 PM |
| How Do I Represent/Convert Floating Point Numbers into IEEE-754 | harryc | Java | 13 | Sep 11th, 2007 2:26 PM |
| How Do I Represent/Convert Floating Point Numbers into IEEE-754 | harryc | C++ | 5 | Sep 3rd, 2007 2:53 PM |
| Floating point range | kurt | Python | 4 | Apr 10th, 2007 4:13 PM |
| how to print floating point numbers | eax | Assembly | 5 | Apr 17th, 2006 7:10 PM |