SW_library: "round" subroutine

rmeldo

Senior Member
Hi forum,

Please let me know if this subject has already been covered in the past.

I was thinking about how to write the code to transform the readings of the readtemp12 command into degC for display and I thought how many people must have written it already and that code like that should be in the "code snippets" section of the forum. Sadly it is not there.

In fact the code snippet section is rather thin.

There must be little subroutines and pieces of code that people write over and over and that should be shared.

Long pieces of code are usually specific to people's project and difficult to share. Small subroutines can be used again and, used only if necessary (the program space is often limited).

I thought that is people started to follow this philosophy and posted their subroutines we all woulb be better off.

One of the requirement should be the ease of search. A way of aiding could be using the message title similar to the one I chose for this one:

SW_library: xxxxxxxxxxxxx.

See below the code for a subroutine which rounds a number to a desired number of digits. It is not fully commented, but short enough to be understood without too many comments. The useful part of the program is the "round" subroutine. The "main" part is for code testing.

Maybe someone could also keep a record (excel?) of the various subroutines posted, so that a library catalog could take shape.

This is my suggestion.... probably with room for improvement.

Riccardo


Code:
symbol this       = w1
symbol digits     = b4
symbol reqDigits  = b5
symbol k          = b6
symbol residual   = b7
main:
   ' --- First test ---
   this      = 31356
   digits    = 4
   reqDigits = 3
   gosub round
   if this <> 3136 then errorSub    ' Expected result
   ' --- Second test ---
   this      = 31356
   digits    = 4
   reqDigits = 2
   gosub round
   if this <> 314 then errorSub     ' Expected result
   ' --- Third test ---
   this      = 314
   digits    = 4
   reqDigits = 1
   gosub round
   if this <> 31 then errorSub      ' Expected result
    ' --- Fourth test ---
   this      = 31356
   digits    = 4
   reqDigits = 0
   gosub round
   if this <> 3 then errorSub       ' Expected result
      
goto main


' This subroutine rounds to a specified number of digits
round:
   
   if digits > 4         then errorSub
   if digits < reqDigits then errorSub

   digits = digits - reqDigits
   for k = 1 to digits
      residual = this//10
      this = this/10
      if residual >= 5 then
         this = this + 1
      endif
   next k
   
return
   

errorSub:

goto errorSub
 

eclectic

Moderator
Hi forum,

Please let me know if this subject has already been covered in the past.

I was thinking about how to write the code to transform the readings of the readtemp12 command into degC for display and I thought how many people must have written it already and that code like that should be in the "code snippets" section of the forum. Sadly it is not there.
Riccardo.

Here's some code that's five years old.

http://www.picaxeforum.co.uk/showthread.php?t=9754&highlight=kitty

Accessed from
Picaxe Home page >> Links >> Peter H. Anderson

Well worth a look.

e
 

rmeldo

Senior Member
Thanks Westaust and Eclectic,

I found and used it. A good one.

However I still think that we should be building up the software section of this forum.

R
 

bgrabowski

Senior Member
I think that the idea of providing code examples may take off if the "wish list" index of handy routines is drafted now, even if there is no code known to be available yet. That will allow forum members to scan through it and quickly find and post routines they have already programmed.

I know I have many routines accumulated over the years which may be useful to others but I have not posted as there is no perceived demand. The more challenging "wish list" routines could be an incentive to the forum heavyweights to innovate. If Rev-Ed wished to provide an incentive, they could put up say a small annual prize for the most useful code published.
 
Top