Doubt about numerical precision
Hi! I'm writing a graphical editor for curves (homework) and I've come with an instance of this algorithm:
Code:
int main(){ 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 :D |
Hi -
Here's a minor change to your program: Code:
#include <stdio.h> Quote:
Why? It's an important question - Google for "C" and "floating point" - I'm sure you'll find the answer. Good luck! |
Nice but...
Thanks! I know it's not my code... and it's not my compiler neither because the original code is in C++ being compiled with g++.
I'm just curious becaise it defies intuition XD Thanks for the interest ;) I'll google that :D |
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. |
Jajajaja I was actually thinking that.
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> Jajajaja nice ah?!?!? |
Hi.
Quote:
Code:
int main(){ Code:
#include <stdio.h> |
Quote:
Code:
for(a = 0.0; a <= 1.0; a = a + 0.05) Unless you are sure internally numbers are represented in decimal. |
Jejeje
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 :D And Sergei Steshenko... Quote:
|
Quote:
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. |
Cool
Yep I agree! ;)
|
All times are GMT -5. The time now is 01:54 PM. |