Strange results with StrToFloat

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Gronque
Posts: 18
Joined: 09 Sep 2021 17:40

Strange results with StrToFloat

#1 Post by Gronque » 02 Nov 2022 21:58

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:
Float.zip
(122.07 KiB) Downloaded 46 times
What am I doing wrong?

Thanks

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Strange results with StrToFloat

#2 Post by filip » 04 Nov 2022 17:24

Hi,

Which version of the compiler are you using ?

Regards,
Filip.

frank.malik
Posts: 96
Joined: 09 Apr 2021 20:37

Re: Strange results with StrToFloat

#3 Post by frank.malik » 06 Nov 2022 09:39

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.
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.malik
Posts: 96
Joined: 09 Apr 2021 20:37

Re: Strange results with StrToFloat

#4 Post by frank.malik » 06 Nov 2022 10:03

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.
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.malik
Posts: 96
Joined: 09 Apr 2021 20:37

Re: Strange results with StrToFloat

#5 Post by frank.malik » 06 Nov 2022 11:50

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).

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

Gronque
Posts: 18
Joined: 09 Sep 2021 17:40

Re: Strange results with StrToFloat

#6 Post by Gronque » 11 Nov 2022 00:25

filip wrote:
04 Nov 2022 17:24
Hi,

Which version of the compiler are you using ?

Regards,
Filip.
Hi filip,

I use version 7.6.0

Regards,
Gronque

Post Reply

Return to “mikroC PRO for PIC General”