Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing print procedure for float variables #197

Open
GoogleCodeExporter opened this issue Mar 3, 2016 · 4 comments
Open

Missing print procedure for float variables #197

GoogleCodeExporter opened this issue Mar 3, 2016 · 4 comments

Comments

@GoogleCodeExporter
Copy link

Since the compiler supports floating point variables, the Print library should 
be extended to print these type of variables.

Original issue reported on code.google.com by robhamerling on 24 Jul 2014 at 9:14

@GoogleCodeExporter
Copy link
Author


I've taken the example provided with the compiler (2.4q2) and modified it a 
little. It depends on the print library, does not support the whole range of 
possible values of a float and should be enhanced to make it generally usable. 

-- ---------------------------------------------------------
-- Simple float to ASCII conversion
-- Notes: - integer part limited to value of an sdword
--        - fraction limited to 6 digits
--
procedure print_float_dec(volatile byte out device, float in n) is

   var byte   d, k
   var sdword sdw

   sdw = n                                         -- integer value
   print_sdword_dec(device, sdw)
   n = n - sdw                                     -- fraction
   device = "."
   k = 1                                           -- at least 1 digit (could be 0)
   repeat
      n = n * 10                                   -- next digit of fraction
      d = byte(n)                                  -- binary value
      device = d + "0"                             -- ASCII to device
      n = n - d                                    -- remainder
      k = k + 1                                    -- count printed digits of fraction
   until ((n < 0.1) | (k > 6))                     -- zero value or more than 6 digits

end procedure

Original comment by robhamerling on 25 Jul 2014 at 7:49

@GoogleCodeExporter
Copy link
Author

Correction: the fraction was not printed correctly with negative floats. Below 
an improved version (still provisional).

-- ---------------------------------------------------------
-- Simple float to ASCII conversion
-- Notes: - integer part limited to value of an sdword
--        - fraction limited to 6 digits
--
procedure print_float_dec(volatile byte out device, float in n) is

   var byte   d, k
   var sdword sdw
   var dword  dwn at n

   sdw = n                                         -- integer value
   print_sdword_dec(device, sdw)
   n = n - sdw                                     -- fraction
   if (n < 0) then
      n = 0 - n
   end if
   device = "."
   k = 1                                           -- at least 1 digit (could be 0)
   repeat
      n = n * 10                                   -- next digit of fraction
      d = byte(n)                                  -- binary value
      device = d + "0"                             -- ASCII to device
      n = n - d                                    -- remainder
      k = k + 1                                    -- count printed digits of fraction
   until ((n < 0.1) | (k > 6))                     -- zero value or more than 6 digits

end procedure

Original comment by robhamerling on 25 Jul 2014 at 8:27

@GoogleCodeExporter
Copy link
Author

The routine above produces a format [-]xxx.xxxxx which supports only a limited 
range of values of a float, but probably sufficient (and desirable!) for many 
purposes, possibly with a parameter to specify the number of digits after the 
decimal point.  
A different format, covering the whole range of values of a float, is 
[-]#.######^[-]##
This format will be needed for applications working with a wider range of 
values than supported by the first format.
Ultimately both formats should be supported. 

Original comment by robhamerling on 28 Jul 2014 at 7:43

@GoogleCodeExporter
Copy link
Author

The code in comment #2 can be simplified to:

-- ---------------------------------------------------------
-- Simple float to  ASCII conversion format:  [-]######.yyyyyy
-- Notes: - integer part limited to value of an sdword
--        - fraction limited to 6 digits
--
procedure print_float_dec(volatile byte out device, float in n) is

   var sdword sdw

   sdw = n                                      -- integer part
   print_sdword_dec(device, sdw)
   n = n - sdw                                  -- keep fraction
   device = "."                                 -- decimal point
   print_sdword_dec(device, 1000000 * n)        -- fraction

end procedure

Original comment by robhamerling on 8 Aug 2014 at 7:30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant