Recent

Author Topic: Formatting a single to display properly  (Read 2195 times)

stephanos

  • New Member
  • *
  • Posts: 38
Formatting a single to display properly
« on: June 06, 2020, 02:59:09 pm »
Dear All

25 years ago I wrote a small database in Pascal for my A Level.  2 months ago I installed Lazarus as I wanted to code a form to create and edit a Playlist (.m3u) file, so as to save me manually writing playlist for my mp3 player.  I completed it and am very happy.  I have also coded in VB6, C, Python and C++, all to a low level and for fun.

I want to explore how to output a single to screen and did the following:
1) Declared a variable
       DeciNum : single;
    which as you will know holds a value from: 0.00000000000000000000000000000000000000000000015, up to 340000000000000000000000000000000000000.0
2) So I made the value of DeciNum the lowest value possible:
      DeciNum := 0.00000000000000000000000000000000000000000000015;
3) Then I attempted to write this value to the screen:
       writeln(  'DeciNum = ', DeciNum);
     However the output was: ‘DeciNum =  0.000000000E+00’
4) I read up on the subject: https://smartpascal.github.io/help/assets/floattostrf.htm
5) Then I tried the following:
       writeln(  'DeciNum = ', FloatToStrF( DeciNum, ffFixed, 2, 46 )  ); 
     However the output was: ‘DeciNum = 0.000000000000000000

Not even the 46 decimal places?
What have I failed to understand?

Thanks, wait to hear

Stephanos
Windows 10, 64 bit
Free Pascal Lazarus Project, version2.0.6

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Formatting a single to display properly
« Reply #1 on: June 06, 2020, 04:09:34 pm »
A single does not have that many places...

Even a double does not have that many places..

and why in tarnation are you interesting in numbers like that ?

Singles = 11-12 digits
doubles= 15-16
Extended = 19-20

and btw, you only get Extended with 32 bit apps... the 64 bit port does not have it.

there are large number libraries out there if that is your interest
The only true wisdom is knowing you know nothing

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Formatting a single to display properly
« Reply #2 on: June 06, 2020, 06:48:49 pm »
Singles have the range of ~-10^-45 to ~10^38 as indicated, but not the precision that the number of zeros that the OP suggests.  FreePascal site suggest 7-8 digits.
https://wiki.freepascal.org/Variables_and_Data_Types 
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

stephanos

  • New Member
  • *
  • Posts: 38
Re: Formatting a single to display properly
« Reply #3 on: June 07, 2020, 01:19:31 am »
Dear Both

Thanks for the feedback.  I am very confused about the maths.  At the website, via the link you provided, a single is explained as having a range of "Single 1.5E-45 .. 3.4E38  7-8  4".  I am not used to this type of notation so I looked it up.  The beginning: 1.5E-45 means I move the decimal place leftward 45 times.  That leads to this long small number.  I agree that that is more than 7-8 decimal places.  But this simply means I do not understand what is the lowest number and highest number in the range of a single, nor do I understand how to determine whatever those numbers are via this type of natation.  I thought it was odd when I set about the task.  So what did I do wrong and what do I not understand?

I am simply trying to do the single version of this

Code: Pascal  [Select][+][-]
  1. Num2 : Shortint;
  2. Num2 := -128;   writeln(Num2);  // Output -128

Luckily I can because I understand -128 and it is not long.  I do not, it would seem, understand 1.5E-45.

Any further knowledge that anyone can impart is appreciated

Thanks, wait to hear

Stephanos
Windows 10, 64 bit
Free Pascal Lazarus Project, version2.0.6

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Formatting a single to display properly
« Reply #4 on: June 07, 2020, 03:09:30 am »
You can map any number in that range into a value of a Single, but it will not be an exact representation because of the limited precision.
So for example. 0.000 000 0 and 0.000 000 01   may convert to the same value of a single precision because it is approximate.  If you need more accuracy, you can move to a double, which has more range and more digits of precision.  In Win32 you can step to extended types as well.
But the number you gave in the original post was something very close to zero , and not close to the limits of singles (ie 1^-45, which is an extremely small number, and 10^38, which is about the age of the universe in nanoseconds ... a very big number).  And of course -1^38 gives the most negative value you can express in a single.
« Last Edit: June 07, 2020, 03:20:56 am by PaulRowntree »
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Formatting a single to display properly
« Reply #5 on: June 07, 2020, 11:01:33 am »
It may be of interest that the write family also supports float formatting.
https://www.freepascal.org/docs-html/rtl/system/write.html

A bit cryptic, but the last part there is maybe helpful.

A small example:
Code: Pascal  [Select][+][-]
  1. begin
  2.   writeln(pi);      // no formatting, E notation
  3.   writeln(pi:1:20); // with formatting
  4. end.
This tries to write pi with one significant and 20 precision.
Those formatting capabilities are often overlooked, but have been there from the start.
(write/writeln and also later writestr)

I often prefer this over formatfloat and it does not need any special unit at all, because this is in system. The above is a complete program.
« Last Edit: June 07, 2020, 11:11:47 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

stephanos

  • New Member
  • *
  • Posts: 38
Re: Formatting a single to display properly
« Reply #6 on: June 09, 2020, 04:26:25 pm »
Thank you both.

I am really sorry that I do not get it yet.

1) Please clarify what this means:
“You can map any number in that range into a value of a Single, but it will not be an exact representation because of the limited precision.”. 
The terms map and representation are probably coding language but I do not know them.

2) Then please clarify what the range of a single is.  Is it:
1.5E-45 .. 3.4E38?
And can the above funny numbers also be written as
0.00000000000000000000000000000000000000000000015 up to 340000000000000000000000000000000000000.0

3) Is the following statement correct: The range of a single does not include negative number?

I want to just establish my understanding this far and deal with why I can’t print out to screen after I have checked this.

Thanks, wait to hear

Stephanos
Windows 10, 64 bit
Free Pascal Lazarus Project, version2.0.6

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Formatting a single to display properly
« Reply #7 on: June 09, 2020, 05:36:17 pm »
Thank you both.

I am really sorry that I do not get it yet.

1) Please clarify what this means:
“You can map any number in that range into a value of a Single, but it will not be an exact representation because of the limited precision.”. 
The terms map and representation are probably coding language but I do not know them.

If you want to get more involved with floating point numbers and their limitations I can only advise you to read e.g. https://en.wikipedia.org/wiki/Floating-point_arithmetic and specifically for the single format https://en.wikipedia.org/wiki/Single-precision_floating-point_format. I know this is lengthy and somewhat mathematical, but if you hang in all your questions will be answered.

2) Then please clarify what the range of a single is.  Is it:
1.5E-45 .. 3.4E38?
And can the above funny numbers also be written as
0.00000000000000000000000000000000000000000000015 up to 340000000000000000000000000000000000000.0

Nearly correct - the smallest sub-normal is 1.4E-45. But a - not all conversion programs (decimally printed to CPU internal binary format) allow conversion of sub-normals. And b - more importantly the value 0.00000000000000000000000000000000000000000000015 is in fact 1.5E-46 and that is allways converted to 0.0! It simply is 10 times smaller than the smallest sub-normal.

3) Is the following statement correct: The range of a single does not include negative number?

No, it is not. A single precision floating point number consists of a sign (+/-) an exponent and the mantissa (aka significant).

I want to just establish my understanding this far and deal with why I can’t print out to screen after I have checked this.

HTH, MathMan

 

TinyPortal © 2005-2018