Sunday, February 13, 2022

TI-84 Plus CE Python (and TI Basic): Rounding vs. Truncating

TI-84 Plus CE Python (and TI Basic):  Rounding vs. Truncating 


Rounding vs Truncating:  Introduction


The program ROUNDING and the Python script rounding.py takes two numbers and estimates one of four arithmetic operations (+, - , ×, ÷).   


Rounding:  rounds a real number to the nearest integer.  The traditional 5/4 rules are applied.  In the case of the integers:  for all numbers with fractional parts of 0.5 and above (to 0.9999...), the integer is rounded up.   


Truncating:  the fractional part of the number is dropped.  


For example:


round(14.2) -> 14

round(14.5) -> 15

round(14.7) -> 15


trunc(14.2) -> 14

trunc(14.5) -> 14

trunc(14.7) -> 14


This results are shown in text form (Python program) or stored in a matrix:


[ [ x and y are rounded individually before the arithmetic operation, percent error ]

[ x and y are truncated individually before the arithmetic operation, percent error ]

[ result is rounded after the arithmetic operation, percent error ]

[ result is truncated after the arithmetic operation, percent error ] ]


The rounding percent program allows the user to compare rounding methods.


TI-84 Plus CE Python:  Python script rounding.py


from math import *

#  rounding comparison

#  2021-12-21 EWS


# rounding

def rd(x):

  return int(x+0.5)


# truncate

def tr(x):

  return int(x)


# percent change

def pc(x,y):

  return (y-x)/x*100


print("integer rounding arithmetic")

pirnt("x [+,-,*,/] y")

x=eval(input("x>0, x? "))

y=eval(input("y>0, y?  "))

print("1 + 2 - 3 * 4 / ")

op=float(input("Operation? "))


# test, force error

if op<1 or op>4:

  print("not a valid operation")

  1/0


# operations

if op==1:

  a=rd(x)+rd(y)

  b=tr(x)+tr(y)

  c=rd(x+y)

  d=tr(x+y)

  z=x+y


if op==2:

  a=rd(x)-rd(y)

  b=tr(x)-tr(y)

  c=rd(x-y)

  d=tr(x-y)

  z=x-y


if op==3:

  a=rd(x)*rd(y)

  b=tr(x)*tr(y)

  c=rd(x*y)

  d=tr(x*y)

  z=x*y


if op==4:

  a=rd(x)/rd(y)

  b=tr(x)/tr(y)

  c=rd(x/y)

  d=tr(x/y)

  z=x/y


print("results")


print("rounding individual")

print(str(a)+" ,"+str(pc(z,a))+"%")


print("truncate individual")

print(str(b)+" ,"+str(pc(z,b))+"%")


print("rounding result")

print(str(c)+" ,"+str(pc(z,c))+"%")


print("truncate result")

print(str(d)+" ,"+str(pc(z,d))+"%")


TI-84 Plus CE Python (TI-Basic):  ROUNDING


"2021-12-21 EWS"

Disp "INTEGER ROUNDING ARITHMETIC","X [+,-,*,/] Y"

Input "X>0, X? ",X

Input "Y>0, Y? ",Y

iPart(X+.5)→N

iPart(X)→S

iPart(Y+.5)→J

iPart(Y)→T

Menu("OPERATION?","X+Y",1,"X-Y",2,"X*Y",3,"X/Y",4)

Lbl 1

N+J→A

S+T→B

X+Y→Z

Goto 5

Lbl 2

N-J→A

S-T→B

X-Y→Z

Goto 5

Lbl 3

N*J→A

S*T→B

X*Y→Z

Goto 5

Lbl 4

N/J→A

S/T→B

X/Y→Z

Goto 5

Lbl 5

iPart(Z+.5)→C

iPart(Z)→D

ClrHome

Disp "ROUND IND","TRUNC INC","ROUND RESULT","TURNC RESULT"

Wait 0.5

Pause [[A,(A-Z)/Z*100][B,(B-Z)/Z*100][C,(C-Z)/Z*100][D,(D-Z)/Z*100]


Examples


17.36+56.19

Rounding Individual:  73, -0.7477906186%

Truncate Individual:  73, -0.7477906186%

Rounding Result:  74, 0.611828688%

Truncate Result: 73, -0.7477906186%


82.8 - 17.9

Rounding Individual:  65, 0.1540832049%

Truncate Individual:  65, 0.1540832049%

Rounding Result:  65, 0.1540832049%

Truncate Result: 64, -1.386748844%


9.28 * 10.26

Rounding Individual:  90, -5.474894132%

Truncate Individual:  90, -5.474894132%

Rounding Result:  95, -0.2234993614%

Truncate Result: 95, -0.2234993614%


46.5 / 1.5

Rounding Individual:  23.5, -24.19354839%

Truncate Individual:  46, 48.38709677%

Rounding Result:  31, 0%

Truncate Result: 31, 0%


It just goes to show, rounding or truncating numbers before calculation may give results that aren't as accurate as one would like.  The following paper, "Rounding versus truncation estimates in difference calculations" by Leonard Van Wyk (see source) argues that sometime truncation is more accurate than rounding in subtraction problems.   


Source


Van Wyk, Leonard   "Rounding versus truncation estimates in difference calculations"  The Mathematical Gazette, Volume 103, Issue 557, July 2019.  pp 285 -292



Eddie 


All original content copyright, © 2011-2022.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Spotlight: Sharp EL-5200

  Spotlight: Sharp EL-5200 As we come on the 13 th (April 16) anniversary of this blog, I want to thank you. Blogging about mathematic...