Board index » cppbuilder » FloatToStr

FloatToStr


2005-02-19 08:05:03 PM
cppbuilder76
I am using Borland C++Builder 5
This is a new problem only seen on my latest computers and upgrades.
I can only look at what is different.
Old computers use Celeron and new ones use P4.
(we are also having problems with another program on computers upgraded from Celeron to P3) 'well that all the motherboards would take'.
Different Microsoft updates
code sample.
String value = InputBox("Input Box", "Enter Value for Pilot Delay ",
FloatToStr(WBSetup->ManuaPilotSeparationDefault));
try
{
WBSetup->ManuaPilotSeparationDefault = StrToFloat(value);
}
catch(const EConvertError &e)
{
ShowMessage("Not A valid floating point number Try Again");
}
NOTE WBSetup values are stored in registry on shutdown.
The problem seems to be with FloatToStr();
A value of 18 should return just "18".
After computer is running for a while it will return
18.00000000061189.
Seems that the value is being down casted to float so the last values are not defined.
A value of 1 returnes
0.9999999998430675
A value of 2 returnes
1.999999999686135.
I have changed the code to use FloatToStringF() but have not tested in on production conmputers.
If I exit program and restart the problem is still threre.
Computer reboot clears problem.
Any help would be usefull
Thanks
Colin
 
 

Re:FloatToStr

Is your 'ManuaPilotSeparationDefault' float? If it is then
define it as double:
double ManuaPilotSeparationDefault;
--
Best regards,
Vladimir Stefanovic
"Colin Hughes" < XXXX@XXXXX.COM >wrote in message
Quote

I am using Borland C++Builder 5

This is a new problem only seen on my latest computers and upgrades.
I can only look at what is different.

Old computers use Celeron and new ones use P4.

(we are also having problems with another program on computers upgraded
from Celeron to P3) 'well that all the motherboards would take'.

Different Microsoft updates

code sample.


String value = InputBox("Input Box", "Enter Value for Pilot Delay ",

FloatToStr(WBSetup->ManuaPilotSeparationDefault));
try
{
WBSetup->ManuaPilotSeparationDefault = StrToFloat(value);
}
catch(const EConvertError &e)
{
ShowMessage("Not A valid floating point number Try Again");
}

NOTE WBSetup values are stored in registry on shutdown.

The problem seems to be with FloatToStr();

A value of 18 should return just "18".
After computer is running for a while it will return

18.00000000061189.

Seems that the value is being down casted to float so the last values are
not defined.

A value of 1 returnes

0.9999999998430675

A value of 2 returnes

1.999999999686135.

I have changed the code to use FloatToStringF() but have not tested in on
production conmputers.

If I exit program and restart the problem is still threre.

Computer reboot clears problem.

Any help would be usefull

Thanks

Colin
 

Re:FloatToStr

Colin Hughes wrote:
Quote
String value = InputBox("Input Box", "Enter Value for Pilot Delay ",
FloatToStr(WBSetup->ManuaPilotSeparationDefault));
WBSetup->ManuaPilotSeparationDefault = StrToFloat(value);
NOTE WBSetup values are stored in registry on shutdown.
Why aren't they stored immediately?
What if the power fails?
Quote
The problem seems to be with FloatToStr();

A value of 18 should return just "18".
After computer is running for a while it will return

18.00000000061189.
How many decimals does your app need?
Use rounding techniques, or use scaled integers.
Quote
Seems that the value is being down casted to float so the last values are not defined.

A value of 1 returnes
0.9999999998430675
Standard rounding errors. Never trust floating point numbers to be
exact.
Quote
If I exit program and restart the problem is still threre.
When you exit, the values are written to registry, and re-read when
restarted? Then not surprising things don't change.
Quote
Computer reboot clears problem.
What registery keys are you reading from?
If you saved the "less exact" values properly, then they should return
after reboot. It sounds like you aren't saving them properly.
 

{smallsort}

Re:FloatToStr

A floating point number consists of a sum of inverse powers of two and
the power of two to which that should be multiplied. Therefore it
consists of sums of one or zero times each of 1/2, 1/4, etc.
Try this as an exercise. Represent 1/10 as an exact sum of inverse
powers of two. It cannot be done.
A float is a small value with a precision of approximately 6 decimal
digits. Your complaint is that it deviated from an exact value in the
12th digit.
By the act of using a float you have declared that you only nominally
care about the exact value. Most functions operate on type double.
When you use them with float values the number is expanded to a double
and the result is converted back to a float. If you have interest in
otehr than nominal accuracy then use doubles instead of floats. If
you have interest in showing a certain number of decimal digits then
use one of the functions which allow you to specify the number of
digits.
Since you are using String, a typedef for AnsiString, look up these
AnsiString functions:
sprintf
Format
cat_printf
cat_sprintf
Information on floating point representation and usage can be found at
these links:
bdn.borland.com/article/0,1410,15855,00.html
docs.sun.com/source/806-3568/ncg_goldberg.html
www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html
www.cs.auckland.ac.nz/~j-hamer/07.211/floats.html
. Ed
Quote
Colin Hughes wrote in message
news:42172b6f$ XXXX@XXXXX.COM ...

I am using Borland C++Builder 5

This is a new problem only seen on my latest computers and upgrades.
I can only look at what is different.

Old computers use Celeron and new ones use P4.

(we are also having problems with another program on computers
upgraded from Celeron to P3) 'well that all the motherboards would
take'.

Different Microsoft updates

code sample.


String value = InputBox("Input Box", "Enter Value for Pilot Delay
",

FloatToStr(WBSetup->ManuaPilotSeparationDefault));
try
{
WBSetup->ManuaPilotSeparationDefault = StrToFloat(value);
}
catch(const EConvertError &e)
{
ShowMessage("Not A valid floating point number Try Again");
}

NOTE WBSetup values are stored in registry on shutdown.

The problem seems to be with FloatToStr();

A value of 18 should return just "18".
After computer is running for a while it will return

18.00000000061189.

Seems that the value is being down casted to float so the last
values are not defined.

A value of 1 returnes

0.9999999998430675

A value of 2 returnes

1.999999999686135.

I have changed the code to use FloatToStringF() but have not tested
in on production conmputers.

If I exit program and restart the problem is still threre.

Computer reboot clears problem.

Any help would be usefull

Thanks

Colin
 

Re:FloatToStr

Hi Ed
I think you have missed to point that this program has worked OK for the last few years and has only started miss behaving after upgrading the computers to use a Pentiunm processor.
We do not use float at all in any of our code, also the program works OK for about an hour before starting to play up
Coming out of the program does not clear the problem but a reboot does.
The problem is the StrToFloat() function used on computers with pentium chips.
As you can see from the return values that it has treated an imput as a float and casted down instead of casting up to long double.
How come it works OK on computers with Celeron chips fitted?
In your list of conversion functions you missed FloatToStrF() which is the function I am using to replace FloatToStr();
Regards
Colin
"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
A floating point number consists of a sum of inverse powers of two and
the power of two to which that should be multiplied. Therefore it
consists of sums of one or zero times each of 1/2, 1/4, etc.

Try this as an exercise. Represent 1/10 as an exact sum of inverse
powers of two. It cannot be done.

A float is a small value with a precision of approximately 6 decimal
digits. Your complaint is that it deviated from an exact value in the
12th digit.

By the act of using a float you have declared that you only nominally
care about the exact value. Most functions operate on type double.
When you use them with float values the number is expanded to a double
and the result is converted back to a float. If you have interest in
otehr than nominal accuracy then use doubles instead of floats. If
you have interest in showing a certain number of decimal digits then
use one of the functions which allow you to specify the number of
digits.

Since you are using String, a typedef for AnsiString, look up these
AnsiString functions:

sprintf
Format
cat_printf
cat_sprintf

Information on floating point representation and usage can be found at
these links:

bdn.borland.com/article/0,1410,15855,00.html
docs.sun.com/source/806-3568/ncg_goldberg.html
www.math.grin.edu/~stone/courses/fundamentals/IEEE-reals.html
www.cs.auckland.ac.nz/~j-hamer/07.211/floats.html

. Ed


>Colin Hughes wrote in message
>news:42172b6f$ XXXX@XXXXX.COM ...
>
>I am using Borland C++Builder 5
>
>This is a new problem only seen on my latest computers and upgrades.
>I can only look at what is different.
>
>Old computers use Celeron and new ones use P4.
>
>(we are also having problems with another program on computers
>upgraded from Celeron to P3) 'well that all the motherboards would
>take'.
>
>Different Microsoft updates
>
>code sample.
>
>
>String value = InputBox("Input Box", "Enter Value for Pilot Delay
>",
>
>FloatToStr(WBSetup->ManuaPilotSeparationDefault));
>try
>{
>WBSetup->ManuaPilotSeparationDefault = StrToFloat(value);
>}
>catch(const EConvertError &e)
>{
>ShowMessage("Not A valid floating point number Try Again");
>}
>
>NOTE WBSetup values are stored in registry on shutdown.
>
>The problem seems to be with FloatToStr();
>
>A value of 18 should return just "18".
>After computer is running for a while it will return
>
>18.00000000061189.
>
>Seems that the value is being down casted to float so the last
>values are not defined.
>
>A value of 1 returnes
>
>0.9999999998430675
>
>A value of 2 returnes
>
>1.999999999686135.
>
>I have changed the code to use FloatToStringF() but have not tested
>in on production conmputers.
>
>If I exit program and restart the problem is still threre.
>
>Computer reboot clears problem.
>
>Any help would be usefull
>
>Thanks
>
>Colin


 

Re:FloatToStr

Quote
I think you have missed to point that this program has worked
OK for the last few years and has only started miss behaving
after upgrading the computers to use a Pentiunm processor.
Yes, I did miss that point.
One thing that you might try is rig a test version which calls
__fpreset() and see if it changes anything.
Borland's floating point implementation uses the full precision of the
floating point processor for calculations. Microsoft sets it to less
precision. If configured the MS way but running code that expects
full precision then problems such as those might be expected. As I
recall, some of the MS common control DLL's reconfigure the floating
point unit in their initialization. _fpreset restores the preceision.
I did not tell you about FloatToStrF because I never noticed that
function.
. Ed
Quote
Colin Hughes wrote in message
news:4219dc62$ XXXX@XXXXX.COM ...

I think you have missed to point that this program has worked
OK for the last few years and has only started miss behaving
after upgrading the computers to use a Pentiunm processor.

We do not use float at all in any of our code, also the program
works OK for about an hour before starting to play up
Coming out of the program does not clear the problem but a
reboot does.

The problem is the StrToFloat() function used on computers
with pentium chips. As you can see from the return values
that it has treated an imput as a float and casted down instead
of casting up to long double.
How come it works OK on computers with Celeron chips fitted?

In your list of conversion functions you missed FloatToStrF() which
is the function I am using to replace FloatToStr();
 

Re:FloatToStr

Sorry for late reply. Verry busy.
I dont use FloatToStr() now as I replaced all calls to FloatToStrF();
I will look up and try your _fpreset() as I would have prefered to not to format the strings that the user enters.
Thanks for your help and time ED, and again sorry for late relply.
Colin
"Ed Mulroy [TeamB]" < XXXX@XXXXX.COM >wrote:
Quote
>I think you have missed to point that this program has worked
>OK for the last few years and has only started miss behaving
>after upgrading the computers to use a Pentiunm processor.

Yes, I did miss that point.

One thing that you might try is rig a test version which calls
__fpreset() and see if it changes anything.

Borland's floating point implementation uses the full precision of the
floating point processor for calculations. Microsoft sets it to less
precision. If configured the MS way but running code that expects
full precision then problems such as those might be expected. As I
recall, some of the MS common control DLL's reconfigure the floating
point unit in their initialization. _fpreset restores the preceision.

I did not tell you about FloatToStrF because I never noticed that
function.

. Ed

>Colin Hughes wrote in message
>news:4219dc62$ XXXX@XXXXX.COM ...
>
>I think you have missed to point that this program has worked
>OK for the last few years and has only started miss behaving
>after upgrading the computers to use a Pentiunm processor.
>
>We do not use float at all in any of our code, also the program
>works OK for about an hour before starting to play up
>Coming out of the program does not clear the problem but a
>reboot does.
>
>The problem is the StrToFloat() function used on computers
>with pentium chips. As you can see from the return values
>that it has treated an imput as a float and casted down instead
>of casting up to long double.
>How come it works OK on computers with Celeron chips fitted?
>
>In your list of conversion functions you missed FloatToStrF() which
>is the function I am using to replace FloatToStr();