 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Johan Elmsäter Guest
|
Posted: Sun Mar 20, 2005 4:13 pm Post subject: Problems with double... |
|
|
I have a problem with double when adding decimal values.
the doubles are assigned with:
double start = (double) StrToFloat(TEdit1->Text);
double inc = (double) StrToFloat(TEdit2->Text);
double stop = (double) StrToFloat(TEdit3->Text);
start could be 0.001 and inc the same. Stop is for example 0.009
When I add inc to start the value isn't 0.001 it could be something like
0.00100000001 instead
and when comparing the value with stop when it should be 0.009 instead it is
0.009000005
What can I do to fix this problem???
Regards Johan
|
|
| Back to top |
|
 |
Analian Guest
|
Posted: Sun Mar 20, 2005 4:53 pm Post subject: Re: Problems with double... |
|
|
Don't relay on double's valus being exactly what you expect (:
Instead of the standart comparing routines you could use:
const double eps = 0.0000001; // you can coustomly choose the precision
// instead of ==
int equal (double a, double b) {
return fabs (a - b) < eps;
}
// instead of >
a > b transforms in a > b + eps
// instead of <
a < b transforms in a < b - eps
Hope this helps.
Analian
|
|
| Back to top |
|
 |
qyte Guest
|
Posted: Sun Mar 20, 2005 5:17 pm Post subject: Re: Problems with double... |
|
|
Johan Elmsäter wrote:
| Quote: | I have a problem with double when adding decimal values.
the doubles are assigned with:
double start = (double) StrToFloat(TEdit1->Text);
double inc = (double) StrToFloat(TEdit2->Text);
double stop = (double) StrToFloat(TEdit3->Text);
start could be 0.001 and inc the same. Stop is for example 0.009
When I add inc to start the value isn't 0.001 it could be something like
0.00100000001 instead
and when comparing the value with stop when it should be 0.009 instead it is
0.009000005
What can I do to fix this problem???
Regards Johan
|
why don't you use this instead?
double start = TEdit1->Text.ToDouble();
double inc = TEdit2->Text.ToDouble();
double stop = TEdit3->Text.ToDouble();
i used it once and had no such problem.
|
|
| Back to top |
|
 |
Palle Meinert Guest
|
Posted: Sun Mar 20, 2005 9:45 pm Post subject: Re: Problems with double... |
|
|
The VCL has a function suited for comparing floating points values. Check
the SameValue function.
/Palle
|
|
| Back to top |
|
 |
Johan Elmsäter Guest
|
Posted: Mon Mar 21, 2005 1:08 pm Post subject: Re: Problems with double... |
|
|
Thanks for all your answers. I have taken a deeper look at it...
and the following code:
double start = 0.01;
double stop = 0.3;
double step = 0.01;
double current = (double) start;
printf("start %20.20g, current: %20.20g, stop: %20.20grn
",start,current,stop);
Generates the following output:
start 0.010000000000000000208, current: 0.010000000000000000208, stop:
0.2999999
999999999889
I have known about problems with floating point values for a long time but
that you can't
make a simple assignment correctly is just too much...
But thanks for your answers though!
Best Regards Johan
"Palle Meinert" <No (AT) Spam (DOT) com> wrote
| Quote: | The VCL has a function suited for comparing floating points values. Check
the SameValue function.
/Palle
|
|
|
| Back to top |
|
 |
qyte Guest
|
Posted: Mon Mar 21, 2005 1:39 pm Post subject: Re: Problems with double... |
|
|
Johan Elmsäter wrote:
| Quote: | double start = 0.01;
double stop = 0.3;
double step = 0.01;
double current = (double) start;
printf("start %20.20g, current: %20.20g, stop: %20.20grn
",start,current,stop);
Generates the following output:
start 0.010000000000000000208, current: 0.010000000000000000208, stop:
0.2999999
999999999889
|
i tried the following code:
double start=0.01;
double stop=0.3;
double step=0.01;
double current = (double) start;
printf("start %f, current: %f, stop: %fn",start,current,stop);
and the result was:
start 0.010000, current: 0.010000, stop: 0.300000
i believe that printf had the problem in your example.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Mar 21, 2005 4:51 pm Post subject: Re: Problems with double... |
|
|
"Johan Elmsäter" <johan.elmsater (AT) bulletinboard (DOT) nu> wrote
| Quote: | double current = (double) start;
|
You are assigning a 'double' to a 'double', so you do not need the cast.
| Quote: | printf("start %20.20g, current: %20.20g, stop: %20.20grn
",start,current,stop);
|
Try using "lg" instead of "g" for the data types. "g" expects a 'float'
whereas "lg" expects a 'double'. A 'float' and a 'double' are not the same
type.
Gambit
|
|
| Back to top |
|
 |
Johan Elmsäter Guest
|
Posted: Sat Mar 26, 2005 5:55 pm Post subject: Re: Problems with double... |
|
|
I changed the code and added my whole testexample and it still doesn't work.
When the loop is finished current should have the value 0.31 but it doesn't
it has the
value of 0.3000000000000000999
The <= operator shows that the textoutput is probably right!
TestDouble() {
double start = 0.01;
double stop = 0.3;
double step = 0.01;
double current = (double) start;
printf("start %20.20lg, current: %20.20lg, stop: %20.20lgrn
",start,current,stop);
while(current <= stop) {
current += step;
printf("current: %20.20lg, stop: %20.20lgrn ",current,stop);
}
}
"Remy Lebeau (TeamB)"
| Quote: |
"Johan Elmsäter" <johan.elmsater (AT) bulletinboard (DOT) nu> wrote in message
news:423ec76d (AT) newsgroups (DOT) borland.com...
double current = (double) start;
You are assigning a 'double' to a 'double', so you do not need the cast.
printf("start %20.20g, current: %20.20g, stop: %20.20grn
",start,current,stop);
Try using "lg" instead of "g" for the data types. "g" expects a 'float'
whereas "lg" expects a 'double'. A 'float' and a 'double' are not the
same
type.
Gambit
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|