-
Notifications
You must be signed in to change notification settings - Fork 0
/
croun.py
122 lines (109 loc) · 3.14 KB
/
croun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import math
from decimal import *
"""
rounding numbers:
for n>0: p>=5 round up
p<5 round down
for n<0: ignore the minus --> do the same as for n>0 --> add the minus again
"""
def rnd(n, decimals=0):
if n>0:
multiplier = 10 ** decimals
m = math.floor(n * multiplier + 0.5) / multiplier
return m
elif n<0:
n = -n
multiplier = 10 ** decimals
m = math.floor(n * multiplier + 0.5) / multiplier
return -m
else:
return 0
"""
rounding numbers down:
for n>0 round down
for n<0 round down
down meaning towards the 0
"""
def drnd(n, decimals=0):
if n>0:
multiplier = 10**decimals
m = math.floor(n * multiplier) / multiplier
return m
elif n<0:
n = -n
multiplier = 10**decimals
m = math.floor(n * multiplier) / multiplier
return -m
else:
return 0
"""
rounding numbers according to needs in physics:
first specified digit: d
if 0<d<3 (d=1, d=2): decimals=1
if d>3: decimals=0
round with rnd(n,decimals=0) accordingly
CAUTION!
THIS SCRIPT MAY NOT BE PERFECT
THERE MIGHT BE SLIGHT ROUNDING ERRORS
"""
def prnd(n):
decimals = 0
while abs(n)<1:
decimals = decimals + 1
n = Decimal(n*10)
while abs(n)>10:
decimals = decimals -1
n = Decimal(n*10)
if 10>abs(n)>=1:
if 0<int(n)<3:
m = drnd(n, 4)
k= 1 + decimals
else:
m = drnd(n,3)
k= 0 +decimals
r = 10**(0 - decimals)
if n>0:
if decimals == 0:
return int(m)
else:
return rnd(m * r, k)
if n<0:
if decimals == 0:
return -int(m)
else:
return rnd(-m * r, k)
else:
return 0
"""
similar to drnd
returns number of digits after comma for correctly rounded numbers after the drnd scheme
"""
def getk(n):
decimals = 0
while abs(n)<1:
decimals = decimals + 1
n = Decimal(n*10)
while abs(n)>10:
decimals = decimals -1
n = Decimal(n*10)
if 10>abs(n)>=1:
if 0<int(n)<3:
k = 1 + decimals
return k
else:
k = 0 + decimals
return k
# TEST FOR rnd
#print("(two decimals) 3.455 rounds to", rnd(3.455, 2), "(should be 3.46)")
#print("(one decimal) 0 rounds to", rnd(0,1), "(should be 0)")
#print("(one decimal) -0.65 rounds to", rnd(-0.65,1), "(should be -0.7)")
#TEST FOR drnd
#print("(1 decimal) 3.99 rounds to", drnd(3.99, 1), "(should be 3.9)")
#print("(one decimal) -5.35 rounds to", drnd(-5.35, 1), "(should be -5.3)")
#print("(four decimals) 0.65 rounds to", drnd(0.65, 4), "(should be 0.65)")
#print(drnd(2.35, 4))
# TEST FOR prnd
#print("0.00235 rounds to", prnd(0.00235), "(should be 0.0024)")
#print("0.65 rounds to", prnd(0.65), "(should be 0.7)")
#print("3.04 rounds to", prnd(3.04), "(should be 3)")
#print("0.234995 rounds to", prnd(0.234995), "(should be 0.23)")