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