I had planned to use StrToFloat to convert the data received via UART but it is difficult to find the right format so I did some tests and got the following result:
What am I doing wrong?
Thanks
Strange results with StrToFloat
Re: Strange results with StrToFloat
Hi,
Which version of the compiler are you using ?
Regards,
Filip.
Which version of the compiler are you using ?
Regards,
Filip.
-
- Posts: 96
- Joined: 09 Apr 2021 20:37
Re: Strange results with StrToFloat
Hi Gronque, hi Filip,
I'm not sure if this helps. I just made a quick check using the Necto Studio v.2.3.0 together with the microC for PIC32 and
the Conversions library. I had exactly the same results !
Looking into the code of the str_to_float function, the "E" will not be parsed in any case. Therefore, the last ten or so strings containing
the "E" will not work anyway. Might be an issue with the multiplier variable in the conversion function, but this is beyond my capabilities.
I'm not sure if this helps. I just made a quick check using the Necto Studio v.2.3.0 together with the microC for PIC32 and
the Conversions library. I had exactly the same results !
Looking into the code of the str_to_float function, the "E" will not be parsed in any case. Therefore, the last ten or so strings containing
the "E" will not work anyway. Might be an issue with the multiplier variable in the conversion function, but this is beyond my capabilities.
Kind regards
Frank
Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2
Frank
Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2
-
- Posts: 96
- Joined: 09 Apr 2021 20:37
Re: Strange results with StrToFloat
Hi,
sorry, my mistake. Both the multiplier and the divider using a int16_t variable. At the same time up to 10 positions before and after the point are
allowed. This means 10^10, this doesn't fit into a int16_t variable and the results are becoming strange.
Basically, this means, everything with a maximum of four digits before and after the point should work. Don't use "E".
If you use a minus value, the number before the point might be reduced to three.
sorry, my mistake. Both the multiplier and the divider using a int16_t variable. At the same time up to 10 positions before and after the point are
allowed. This means 10^10, this doesn't fit into a int16_t variable and the results are becoming strange.
Basically, this means, everything with a maximum of four digits before and after the point should work. Don't use "E".
If you use a minus value, the number before the point might be reduced to three.
Kind regards
Frank
Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2
Frank
Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2
-
- Posts: 96
- Joined: 09 Apr 2021 20:37
Re: Strange results with StrToFloat
Hi,
and again I overlooked something. The multiplier was ok already, and set to uint32_t resp uint64_t. The issue is only the divider.
After setting the divider to uint32_t it's possible to convert float-strings with up to 9 digits after the point correctly (at least as far as I can see).
and again I overlooked something. The multiplier was ok already, and set to uint32_t resp uint64_t. The issue is only the divider.
After setting the divider to uint32_t it's possible to convert float-strings with up to 9 digits after the point correctly (at least as far as I can see).
Code: Select all
FloatVar = str_to_float("-123.123"); // FloatVar is -1.231230E+002, ok
FloatVar = str_to_float("0.000000001"); // FloatVar is 1.000000E-009, ok
FloatVar = str_to_float("123456789.0123456789"); // FloatVar is 0.000000E+000, too many digits after the point
FloatVar = str_to_float("123456789.123456789"); // FloatVar is 1.234568E+008, ok
FloatVar = str_to_float("12345.6789"); // FloatVar is 1.234568E+004, ok
FloatVar = str_to_float("0.0000000001"); // FloatVar is 0.000000E+000, too many digits after the point
FloatVar = str_to_float("0.0000000012"); // FloatVar is 0.000000E+000, too many digits after the point
FloatVar = str_to_float("0.000000012"); // FloatVar is 1.200000E-008, ok
FloatVar = str_to_float("0.00000012"); // FloatVar is 1.200000E-007, ok
FloatVar = str_to_float("0.0000012"); // FloatVar is 1.200000E-006, ok
FloatVar = str_to_float("0.000012"); // FloatVar is 1.200000E-005, ok
FloatVar = str_to_float("0.00012"); // FloatVar is 1.200000E-004, ok
FloatVar = str_to_float("0.0012"); // FloatVar is 1.200000E-003, ok
FloatVar = str_to_float("0.00123"); // FloatVar is 1.230000E-003, ok
Kind regards
Frank
Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2
Frank
Fusion for STM32 v8, STM32F407ZG@168MHz, 4" TFT capacitive
mikromedia 3, PIC32MZ2048EFH100@200MHz, 3" TFT capacitive
NECTO Studio 3.0.0, mikroC AI for ARM/PIC32, mikroSDK v.2.7.2