<div dir="ltr"><div><div>Oops:<br></div>The third paragraph should read: <br></div>In like manner, IF THE RIGHT HAND DIGIT IS <3,<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Dec 10, 2013 at 10:24 AM, Louis Bogdan <span dir="ltr"><<a href="mailto:looiebwv@gmail.com" target="_blank">looiebwv@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div>It seems that I'm having a problem in getting my point across as to what I would like to do, so let's try another approach.<br>
<br></div>I have a number, 1.2348 and it is of no consequence or interest where it came from, etc.  I would like to manipulate the right hand most digit so that if it is greater that 7, I would increment it by 2 to 0 and end up with the number being either 1.2350 or 1.2351.  <br>

<br></div>In like manner I would just change the right hand most digit to 0 as this would have no effect on any other digits, I would end up with the number being 1.2340. <br><br></div>Now if the right hand digit was not greater than 7 nor less than 3, it fall into the range of 3-8 and digits 3, 4, 5 ,6 &7 I change to digit 5<br>

<br></div>The end result of the above I will have a number whose right hand digit will be either  0 or 5.  And here again, what happens next is of no consequence or interest to anyone but me.  But to satisfy some people's curiosity, the above number was derived from a keypad input, two integer four decimal, and manipulated by an internal equation.  The finally derived number will then manipulated into an integer which will the number of steps required to make a linear movement equal to the above number of inches.<br>

<br></div>NOW!! HOW DO I SAY THAT IN PYTHON?<br><div><div><div><div><div><br><br></div></div></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Dec 10, 2013 at 8:22 AM, Neil Ludban <span dir="ltr"><<a href="mailto:nludban@columbus.rr.com" target="_blank">nludban@columbus.rr.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, 9 Dec 2013 18:59:01 -0500<br>
Louis Bogdan <<a href="mailto:looiebwv@gmail.com" target="_blank">looiebwv@gmail.com</a>> wrote:<br>
...<br>
<div>> > On Mon, Dec 9, 2013 at 12:56 PM, Louis Bogdan <<a href="mailto:looiebwv@gmail.com" target="_blank">looiebwv@gmail.com</a>> wrote:<br>
</div><div>> >> Now I know that Python does rounding which I don't understand and have<br>
> >> not found any decent, understandable explanation how it does it.  I<br>
> >> understand rounding, even way back when I was proficient with the abacus.<br>
<br>
</div>It's really quantization errors, not rounding.  If binary integers<br>
are expressed as a sum of numbers from the set {1, 2, 4, 8, 16, ...}<br>
then binary floating point numbers (from a very simplistic viewpoint)<br>
add to that set the fractions {1/2, 1/4, 1/8, 1/16, ...}.  64-bit<br>
doubles (the typical Python "float" type) enables 53 consecutive<br>
values from that combined set:<br>
<br>
<a href="http://en.wikipedia.org/wiki/Double_precision_floating-point_format" target="_blank">http://en.wikipedia.org/wiki/Double_precision_floating-point_format</a><br>
<br>
So numbers like 1, 1+1/2, 1+1/4, 1+1/8, can be represented exactly<br>
in decimal and binary (the minus sign is added here to force the<br>
bin function to output all 64 bits):<br>
<br>
--> import struct<br>
--> bin(struct.unpack('Q', struct.pack('d', float('-1.0000')))[0])<br>
'0b1011111111110000000000000000000000000000000000000000000000000000'<br>
--> bin(struct.unpack('Q', struct.pack('d', float('-1.5000')))[0])<br>
'0b1011111111111000000000000000000000000000000000000000000000000000'<br>
--> bin(struct.unpack('Q', struct.pack('d', float('-1.2500')))[0])<br>
'0b1011111111110100000000000000000000000000000000000000000000000000'<br>
--> bin(struct.unpack('Q', struct.pack('d', float('-1.1250')))[0])<br>
'0b1011111111110010000000000000000000000000000000000000000000000000'<br>
<br>
<br>
1+1/10 works in decimal, but is a repeating pattern in binary:<br>
--> bin(struct.unpack('Q', struct.pack('d', float('-1.1000')))[0])<br>
'0b1011111111110001100110011001100110011001100110011001100110011010'<br>
<br>
<br>
1+1/3 repeats in both decimal and binary:<br>
--> -4/3.<br>
-1.3333333333333333<br>
--> bin(struct.unpack('Q', struct.pack('d', float(-4/3.)))[0])<br>
'0b1011111111110101010101010101010101010101010101010101010101010101'<br>
<br>
<br>
And then there's the weird stuff that may concern you:<br>
<br>
--> bin(struct.unpack('Q', struct.pack('d', float(-4/3. * 1000 / 1000)))[0])<br>
'0b1011111111110101010101010101010101010101010101010101010101010101'<br>
--> bin(struct.unpack('Q', struct.pack('d', float(-4/3. + 1000 - 1000)))[0])<br>
'0b1011111111110101010101010101010101010101010101010101011000000000'<br>
--> -4/3. + 1000 - 1000<br>
-1.3333333333333712<br>
<br>
The result of the last example can vary depending on the compiler<br>
and optimization flags, whether the code runs on the main FPU or<br>
vector coprocessor (eg, MMX), level of IEEE-754 compliance (which<br>
varies from none for early Crays to partly for early NVIDIA GPUs to<br>
full for most modern desktops), and any non-standard options (eg,<br>
rounding mode and denormal truncation) which other parts of your<br>
application or supporting libraries may have set...<br>
<br>
<br>
Hope that helps.<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>