FloatToStr with more decimal places displayed ?

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Author
Message
Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: FloatToStr with more decimal places displayed ?

#16 Post by Soumitrab » 28 Jan 2023 16:21

Janni - the single precision floating point is derived by a strtofloat conversion for storage and transmission as the 4 bytes of a floating point is smaller than a string literal.
So that's my primary concern. Upto this point everything is fine but what is method to extract a floattostr with more decimal places in the fractional part. ?
My method works perfectly in Delphi but the same does not on the pic. Yes in both cases i use a single/real aa the FP.
If there is a way to extract the fractional part without any operation or loss of accuracy then I'd like to know.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: FloatToStr with more decimal places displayed ?

#17 Post by janni » 28 Jan 2023 22:04

Soumitrab wrote:
28 Jan 2023 16:21
Janni - the single precision floating point is derived by a strtofloat conversion for storage and transmission as the 4 bytes of a floating point is smaller than a string literal.
If I understand correctly, there was an accurate number stored in a string, then converted to floating-point representation and you need to restore the original accurate number back to string? As stated earlier, the conversion process means loss of accuracy that cannot be reversed. But the conversion may be replaced by another compacting method, not necessarily involving floating-point numbers.
So that's my primary concern. Upto this point everything is fine but what is method to extract a floattostr with more decimal places in the fractional part. ?
And now you've got me confused. Is the number of decimal places or the accuracy a problem?
My method works perfectly in Delphi but the same does not on the pic. Yes in both cases i use a single/real aa the FP.
If there is a way to extract the fractional part without any operation or loss of accuracy then I'd like to know.
And I stay confused - single precision, even in different format (Delphi uses IEEE format), means the same limitation of accuracy and you suggest that Delphi plays miracles and packs more information into 4 bytes than possible. Or is it just the number of decimal places problem, whether they're meaningful or not?

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: FloatToStr with more decimal places displayed ?

#18 Post by AntiMember » 28 Jan 2023 22:21

Transmit the integer and fractional parts of the number separately.
The number of bytes will increase to 8, the number of digits in the fractional part to 7.
Up to 7 significant digits. Regardless of the integer part of the number.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: FloatToStr with more decimal places displayed ?

#19 Post by janni » 29 Jan 2023 03:02

AntiMember wrote:
28 Jan 2023 22:21
Transmit the integer and fractional parts of the number separately.
The fractional part may be stored as an integer as well - there's no need of conversion to fp. Dividing the input string (not necessarily at the decimal point if the range of numbers has an upper limit) and converting the two parts to 32 bit integers should suffice.

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: FloatToStr with more decimal places displayed ?

#20 Post by Soumitrab » 29 Jan 2023 07:04

And I stay confused - single precision, even in different format (Delphi uses IEEE format), means the same limitation of accuracy and you suggest that Delphi plays miracles and packs more information into 4 bytes than possible. Or is it just the number of decimal places problem, whether they're meaningful or not?
I didn't say anything about Delphi performing miracles. I am saying that while Delphi allows for the fractional part to be extracted correctly using a single prec. FP, the same operation on a pic yields a different result.
Probably because we're not using the IEEE format?

So obviously the data width is not the limitation.

For example:
If x = 5.000879965, then getting the fractional part by this operation in Delphi yields a different result.
F (as dword) = x - int(x).
However in Delphi this yields the correct result :
F = (x ) - ( int(x) * 1.0000000)
But not quite on the pic.

In fact let me post the routine tomorrow, I don't have my computer with me today.

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: FloatToStr with more decimal places displayed ?

#21 Post by Soumitrab » 29 Jan 2023 07:11

AntiMember wrote:
23 Jan 2023 17:39
I think it's easier to upgrade to a more powerful processor. Writing a float transformation is not at all an easy task.
It's not difficult either, i use a customized routine myself in existing projects and it works flawlessly.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: FloatToStr with more decimal places displayed ?

#22 Post by AntiMember » 29 Jan 2023 09:30

janni wrote:
29 Jan 2023 03:02
The fractional part may be stored as an integer as well - there's no need of conversion to fp. Dividing the input string (not necessarily at the decimal point if the range of numbers has an upper limit) and converting the two parts to 32 bit integers should suffice.
No problem.
If the source string has a fixed number of digits that will fit in 2x4 bytes.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: FloatToStr with more decimal places displayed ?

#23 Post by AntiMember » 29 Jan 2023 09:39

Soumitrab wrote:
29 Jan 2023 07:04
F (as dword) = x - int(x).
However in Delphi this yields the correct result :
F = (x ) - ( int(x) * 1.0000000)
But not quite on the pic.
And in Pascal for PIC there is no modf?

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: FloatToStr with more decimal places displayed ?

#24 Post by Soumitrab » 29 Jan 2023 13:38

AntiMember wrote:
29 Jan 2023 09:39
And in Pascal for PIC there is no modf?
Ohh excellent catch. I totally forgot about this function. Have to try it now. Thanks a ton!! 😊

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: FloatToStr with more decimal places displayed ?

#25 Post by janni » 29 Jan 2023 17:28

Soumitrab wrote:
29 Jan 2023 07:04
I didn't say anything about Delphi performing miracles.
No, that's what I wrote in exasperation as there are no miracles in arithmetic :).
I am saying that while Delphi allows for the fractional part to be extracted correctly using a single prec. FP, the same operation on a pic yields a different result.
Probably because we're not using the IEEE format?

So obviously the data width is not the limitation.
It certainly is but now I see why we did not understand each other :( . You are convinced that the impossible took place and don't see why it could not be repeated.
In fact let me post the routine tomorrow
That would be best as result of

Code: Select all

var x,y:single;

x:= 5.000879965;
y:= x - int(x);
in Delphi is 0.00087976455688, as expected from single precision.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: FloatToStr with more decimal places displayed ?

#26 Post by janni » 29 Jan 2023 17:31

AntiMember wrote:
29 Jan 2023 09:39
And in Pascal for PIC there is no modf?
This function performs exactly the same operation we're discussing.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: FloatToStr with more decimal places displayed ?

#27 Post by AntiMember » 29 Jan 2023 18:32

AntiMember wrote:
28 Jan 2023 22:21
Transmit the integer and fractional parts of the number separately.
The number of bytes will increase to 8, the number of digits in the fractional part to 7.
Up to 7 significant digits. Regardless of the integer part of the number.
And if the integer part of the number is always less than 65535, you can put it in 6 bytes.
Last edited by AntiMember on 29 Jan 2023 20:15, edited 2 times in total.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: FloatToStr with more decimal places displayed ?

#28 Post by AntiMember » 29 Jan 2023 18:34

janni wrote:
29 Jan 2023 17:31
This function performs exactly the same operation we're discussing.
I understand that the accuracy was lost when strtofloat conversion...
But it's beautiful - modf! 8)

Soumitrab
Posts: 156
Joined: 08 Jan 2012 07:28

Re: FloatToStr with more decimal places displayed ?

#29 Post by Soumitrab » 31 Jan 2023 06:39

Yes, modf is exactly what im looking for.

Thanks @janni, @antimember.

jazzybeyyz@gmail.com
Posts: 1
Joined: 05 Mar 2024 12:02

Re: FloatToStr with more decimal places displayed ?

#30 Post by jazzybeyyz@gmail.com » 07 Mar 2024 09:35

I totally forgot about this function. Have to try it now. Thanks a ton!! 😊 waplus
projectfreetv.onl

Post Reply

Return to “PIC PRO Compilers”