Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
OK, i have a script, which calculate ...
Scripts are not discussed in this Forum.
There is a Visual C++ Programming forum, and Visual C++ is code, not script.
Quote:
Originally Posted by
justutiz
... which calculate Y=1/(C-A). C is input, A is intetrval [Ap;Ag] with step Az;
Did you, perhaps, mean "A is a value within intetrval [Ap;Ag]"?
Anyway, have a look at:
Floating point
What Every Computer Scientist Should Know About Floating-Point Arithmetic
and a lot of other articles/discussions here
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
VictorN
Im reading, and Im very new in C++, so I dont find how I can do the folowing that a == c, then a and c is floats.
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
... so I dont find how I can do the folowing that a == c, then a and c is floats.
In general you cannot just compare floats as "a == c". You have to implement some relative small (with respect to the "a" and "c") value ("delta") and compare like:
Code:
if(abs(a - c) < delta)
{
...
}
Re: 0 isn't 0 in C++ math
but abs want me to use int, my a and c is floats
Re: 0 isn't 0 in C++ math
Well, I meant this abs, _abs64
However, you can use fabs instead.
Re: 0 isn't 0 in C++ math
Quote:
when i want to say that 0 isn't possible in 1/0, the program colculate me: Y = -2.1e+06
Result of 1/0 is infinity, while programming languages operate with only finite figures. So you have the best result FPU was able to provide. :)
Re: 0 isn't 0 in C++ math
Im still geting
Code:
1 -1 0
1.11 -0.9 0
1.25 -0.8 0
1.43 -0.7 0
1.67 -0.6 0
2 -0.5 0
2.5 -0.4 0
3.33 -0.3 0
5 -0.2 0
10 -0.1 0
-1.34e+07 7.45e-08 0
-10 0.1 0
-5 0.2 0
-3.33 0.3 0
-2.5 0.4 0
-2 0.5 0
-1.67 0.6 0
-1.43 0.7 0
-1.25 0.8 0
-1.11 0.9 0
trying everything, and still dont get how to fix it.
Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <math.h>
using namespace std;
int main ()
{
float y, c, ap, ag;
c = 0;
ap = -1;
ag = 1;
float az = 0.1;
if (ag > ap && az > 0.0) {
for (float a = ap; a <= ag; a += az) {
if ( fabsf(c - a) < 0.0 && fabsf(c - a) > 0.0 ) {
cout << "1/0 not posible";
}
else {
y = 1.0 / (c - a);
cout << setprecision(4) << y <<" " << a << " " << c << endl;
}
}
}
else cout << "STOP";
system ("pause");
return 0;
}
Re: 0 isn't 0 in C++ math
It's impossible to succeed with the condition fabsf(c - a) < 0.0. Victor meant something like fabsf(c - a) < 0.00001.
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
Igor Vartanov
Result of 1/0 is infinity, while programming languages operate with only finite figures. So you have the best result FPU was able to provide. :)
Yes, but why in my code -0.1 + 0.1 is not 0?
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
Igor Vartanov
It's impossible to succeed with the condition fabsf(c - a) < 0.0. Victor meant something like fabsf(c - a) < 0.00001.
Yes, it works, but maybe it is shortes way to compare c-a is 0?
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
Yes, but why in my code -0.1 + 0.1 is not 0?
I don't know what's wrong with your code, but mine works fine:
Code:
#include <stdio.h>
int main()
{
float a = -0.1;
printf("%f", a +0.1);
return 0;
}
Quote:
D:\Temp\8>8.exe
-0.000000
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
Igor Vartanov
I don't know what's wrong with your code, but mine works fine:
Code:
#include <stdio.h>
int main()
{
float a = -0.1;
printf("%f", a +0.1);
return 0;
}
when add one time its true, but when you do it with cycle it looks something like this
a = -0.1 + 0.01000000001 and so on
and then you compare 0 with a it is looks something like this 0 != 0.0000000000001
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
Yes, it works, but maybe it is shortes way to compare c-a is 0?
Well, the big mistake you make is that you think math and programming are the same. Floats in programming have finite precision, so you must take certain precautions and tricks to succeed with your calculations. Like comparing with 0.00001, for example.
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
when add one time its true, but when you do it with cycle it looks something like this
a = -0.1 + 0.01000000001 and so on
and then you compare 0 with a it is looks something like this 0 != 0.0000000000001
Yep, see above: float calculations have finite precision. And the result might fluctuate a little. :)
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
when add one time its true, but when you do it with cycle it looks something like this
a = -0.1 + 0.01000000001 and so on
and then you compare 0 with a it is looks something like this 0 != 0.0000000000001
Haven't you read yet what I referred to in the post#2?
Re: 0 isn't 0 in C++ math
so
Code:
if ( fabsf(c - a) < 0.000001 )
is the best and shortest way?
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
so
Code:
if ( fabsf(c - a) < 0.000001 )
is the best and shortest way?
I'd say it is the only way. The question is only about this 'delta' value. In some case your choice with 0.000001 can work, in other - not. It all depends on the problem / values you are working on / with.
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
VictorN
I'd say it is the only way. The question is only about this 'delta' value. In some case your choice with 0.000001 can work, in other - not. It all depends on the problem / values you are working on / with.
so it is posible to make working with all values, if az i take 0.000000001 then it is not working, so i need to add more 0.00000000000000001 in compare, it is other way?
Re: 0 isn't 0 in C++ math
Again: did you read about FP arithmetic?
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
so it is posible to make working with all values, if az i take 0.000000001 then it is not working, so i need to add more 0.00000000000000001 in compare, it is other way?
Before you ask more questions, please read this:
http://www.parashift.com/c++-faq-lit...html#faq-29.16
http://www.parashift.com/c++-faq-lit...html#faq-29.17
http://www.parashift.com/c++-faq-lit...html#faq-29.18
Computers work in binary, not decimal. This means that the decimal numbers you're trying to use must be converted to binary. Do you know how to convert a decimal floating point value to a binary floating point value? If you do, then you will see why decimal fractions cannot have exact binary representations unless they are a sum of inverse powers of 2.
If you want exact numbers, then don't use float or double. Use a class that represents arbitrary precision numbers, and there are many of them out there.
Regards,
Paul McKenzie
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
VictorN
Again: did you read about FP arithmetic?
Yes, but dont understand everything. I just need to do a program in few days. This is my first program and I am still learning everything.
Re: 0 isn't 0 in C++ math
Quote:
Originally Posted by
justutiz
Yes, but dont understand everything. I just need to do a program in few days. This is my first program and I am still learning everything.
Well, this code will not work:
Code:
for (float a = ap; a <= ag; a += az)
Never use floats or doubles as loop counters. Never.
The reason is that you cannot guarantee how many times that loop will execute. It could execute 1 more time than expected, and the reason was explained by everyone so far -- floating point computations are not exact.
Always use integers as loop counters. This guarantees that the loop will execute the exact number of times you expect. For example, if you know you should loop from 0.01 to 0.1 then you write this:
Code:
for (int i = 1; i <= 100; ++i)
{
double myValue = i / 100.0;
// then you use myValue in the loop, not i
//...
}
Now the loop is guaranteed to loop 100 times. The bottom line is however you do it, the goal is to normalize all "counted" loops to use integers as loop counters.
Regards,
Paul McKenzie