OK, I understand what is being explained.
However I don't really agree.
bgrabowski
The true result of the calculation 5025/1000 is not 5 but 5.025 - integer math results in this becoming 5 but the underlying number 5025 is there.
The true remainder of the sample calculation 5025//1000 is actually 025 not 25 - after all if if the starting number is 5125, the remainder is 125, not 25.
The issue is that the leading zero is dropped in the calculation before it gets to the display. This can be verified in the simulator.
Hippy
your example deals with the display issues but with the // operator, the problem is in the math, before it gets to the display - as above - this is verified in the simulator
I guess we have to work with what we have so I suggest the explanation in the manual be improved to cover this as when looking at the way many people use this function in their programs they have fallen into the same trap!
e.g. Hippy, your code snippet;
Code:
Symbol Nref = b0
Symbol Nadc = b1
Symbol Vadc = b2
Symbol Vdec = b3
CalibAdc Nref
ReadAdc 1, Nadc
Vadc = Nadc * 6 / Nref / 10
Vdec = Nadc * 6 / Nref // 10
SerTxd( "Voltage = ", #Vadc, ".", #Vdec, "V", CR, LF )
Only works correctly as it only outputs one decimal place but if you were attempting to display 2 decimal places (Vdec = Nadc * 60 / Nref // 100) it would produce errors.
So what is the workaround (if you don't have an X1 that supports DIG)?
How could 5025 be displayed as 5.025?
OK just discovered (a case of serendipity) your post #8 here;
http://www.picaxeforum.co.uk/showthread.php?t=8092
Code:
Watch out if going to two or more digits after the decimal point ...
- w0 = value / 1000
- w1 = value // 1000
- SerTxd( #w0, ".", #w1, CR, LF ) ' Display X.YYY
Leading zeroes get dropped in #w1 so a value of 1001 will end up incorrectly reported as "1.1", not as "1.001".
To get round that it becomes necessary to force those leading zeroes to be reported separately ...
- w0 = value / 1000
- w1 = value // 1000
- SerTxd( #w0, "." )
- If w1 < 100 Then : SerTxd( "0" ) : End If
- If w1 < 10 Then : SerTxd( "0 ") : End If
- SerTxd( #w1, CR, LF )
An arguably more elegant version ( but probably with slightly larger code size ) would be -
- w0 = value / 1000
- w1 = value // 1000
- SerTxd( #w0, "." )
- Select Case w1
--- Case < 100 : SerTxd( "00" )
--- Case < 10 : SerTxd("0")
- End Select
- SerTxd( #w1, CR, LF )
The best way to save code space if you have a number of decimal numbers to display is to put that code in a subroutine and call it as needed.
Thanks - nothing new under the sun