ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi! I'm writing a graphical editor for curves (homework) and I've come with an instance of this algorithm:
Code:
int main(){
float a;
for(a = 0.0; a <= 1.0; a = a + 0.05)
printf("%f\n", a);
return 0;
}
Now the thing is that it doesn't print 1.0 XD Now I'd like some opinions about the implications of numerical precision here because its just additions and besides a is an increasing number.
I tried it with "double a;" but it just the same jejeje. I'm using gcc 4.1.0.
Hint: this is pure "C" (and floating point precision). It has nothing to do with you, your program, or GCC (in fact, I compiled/ran this under Visual Studio. You'll get similar results with ANY C compiler).
Why?
It's an important question - Google for "C" and "floating point" - I'm sure you'll find the answer.
The value you use as an increment may not be exactly representable as a floating point number. In the 80's I read an article in a C-Language magazine about floating point numbers. There were so many caveats, I couldn't believe how complicated it was. A floating point library may even use approximation routines for sin() for very small angles. The routine to calculate the value depends on the input value, and isn't always a series. ( The same thing may be be happening in a floating point processor )
If you evaluate some series, you may need to process the series so that the smaller values are added first. This may mean a for loop that counts down for some series.
Never test for equality in floating point numbers. Test if the value is within a small delta from the value you want to test. Using a for loop, you might want to add a small delta value to the top value in your loop.
When I took Numerical Analysis for CS at university I remember I had to evaluate series from the smallest value to the biggest I actually have those proof somewhere in my notes.
The funny thing is that if you re write it like this:
Code:
#include <stdio.h>
int main(){
float a;
for(a = 0.0; a <= 1.0;){
printf("%f\n", a);
a+=0.05;
}
return 0;
}
So you can change the position of the a+= 0.05. If it is after the print (like here) you loose the 1.0 but if it is before you loose the 0.0
Hi! I'm writing a graphical editor for curves (homework) and I've come with an instance of this algorithm:
Code:
int main(){
float a;
for(a = 0.0; a <= 1.0; a = a + 0.05)
printf("%f\n", a);
return 0;
}
Now the thing is that it doesn't print 1.0 XD Now I'd like some opinions about the implications of numerical precision here because its just additions and besides a is an increasing number.
I tried it with "double a;" but it just the same jejeje. I'm using gcc 4.1.0.
I'm pretty curious about this
Absolutely never write such loops:
Code:
for(a = 0.0; a <= 1.0; a = a + 0.05)
in any programming language.
Unless you are sure internally numbers are represented in decimal.
Hey ErV... nice approach with the casts... and defining the epsilon... cool. I think I'm gonna' steal you the one with the casts jejejeje because is pretty elegant
What I did was, to let the for stop by its own imprecision and then by taking advantage of a math property of the curves I'm viewing, to make a final assignment and problem solved... it's almost like if my epsilon is defined so I can stop exactly one number earlier
And Sergei Steshenko...
Quote:
Unless you are sure internally numbers are represented in decimal.
You mean... that I have to be sure that the real number can actually be represented using the given precision?
Hey ErV... nice approach with the casts... and defining the epsilon... cool. I think I'm gonna' steal you the one with the casts jejejeje because is pretty elegant
What I did was, to let the for stop by its own imprecision and then by taking advantage of a math property of the curves I'm viewing, to make a final assignment and problem solved... it's almost like if my epsilon is defined so I can stop exactly one number earlier
And Sergei Steshenko...
You mean... that I have to be sure that the real number can actually be represented using the given precision?
Not given precision, but infinite precision for the fraction you are using as step and for loop limits.
Such loops are a disaster in general case because you can't be sure how many times they are executed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.