-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalcModes.py
177 lines (161 loc) · 6.49 KB
/
calcModes.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#***********************************************************************
# calcModes.py
# Copyright (C) 2018 - 2019 Scott Schoeller (sschoellerSTEM)
#
# This file is part of PhySciCalc
# PhySciCalc is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# PhySciCalc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with PhySciCalc. If not, see <https://www.gnu.org/licenses/>.
#***********************************************************************
import numpy
from ext import *
class calcModes:
# answer will store the current answer
answer = 0.0
def arith(self, mode):
'''arith(self, mode) computes the arithmetic operations
addition (mode= add), substraction (mode= sub),
multiplication (mode= mult), division (mode= div),
square root (mode= sqrt)'''
done = '' # dummy variable
num = []
numio = 0.0
if mode == 'add':
while done == '':
try:
numio = float(input('Type a non-zero number to add or simply enter 0 to compute and exit: '))
if numio != 0.0: # adding anything to 0 does not affect the final result
num.append(numio)
else:
break
except ValueError:
continue # handles input that can't be converted
for number in num:
numio = numio + number
answer = numio
return answer
elif mode == 'sub':
while done == '':
try:
tmp = float(input('Type a non-zero number to subtract or simply enter 0 to compute and exit: '))
if tmp != 0.0: # subtracting anything by 0 does not affect the final result
num.append(tmp)
else:
break
except ValueError:
continue # handles input that can't be converted
for number in num:
numio = number - numio
answer = - numio
return answer
elif mode == 'mult':
while done == '':
try:
tmp = float(input('Type a non-zero number to multiply or simply enter 0 to compute and exit: '))
if tmp != 0.0: # multiplying anything by 0 is from 0
num.append(tmp)
else:
break
except ValueError:
continue # handles input that can't be converted
for number in num:
numio = numio * number
answer = numio
return answer
elif mode == 'div':
num = float(input('Enter any number: '))
divnum = float(input('Enter a non-zero number to divide by or simply enter 0 to cancel: '))
if divnum != 0.0: # can't divide by 0
return num/divnum
else:
return 0.0
elif mode == 'sqrt': # make sure to handle complex numbers
try:
numio = complex(input('Enter any number: '))
except ValueError:
return 'Error: invalid input!'
# else, return the sqrt
answer = numpy.sqrt(numio)
return answer
def trig(self, mode):
'''trig(self, mode) handles sin, cos, tan, cot, sec, csc'''
numio = 0.0
if mode == 'sin':
try:
numio = float(input('Enter an angle in radians: '))
except ValueError:
return 'ERROR: invalid input!'
# if ValueError *NOT* encountered
answer = numpy.sin(numio)
return answer
elif mode == 'cos':
try:
numio = float(input('Enter an angle in radians: '))
except ValueError:
return 'ERROR: invalid input!'
# if ValueError *NOT* encountered
answer = (numpy.cos(numio))
return answer
elif mode == 'tan':
try:
numio = float(input('Enter an angle in radians: '))
except ValueError:
return 'ERROR: invalid input!'
# if ValueError *NOT* encountered
answer = numpy.tan(numio)
return answer
elif mode == 'cot':
try:
numio = float(input('Enter an angle in radians: '))
except ValueError:
return 'ERROR: invalid input!'
# if ValueError *NOT* encountered
if numio != 0.0:
answer = 1/(numpy.tan(numio))
return answer
else:
return 'ERROR: can not divide by 0'
elif mode == 'sec':
try:
numio = float(input('Enter an angle in radians: '))
except ValueError:
return 'ERROR: invalid input!'
# if ValueError *NOT* encountered
if numio != 0.0:
answer = 1/(numpy.cos(numio))
return answer
else:
return 'ERROR: can not divide by 0'
elif mode == 'csc':
try:
numio = float(input('Enter an angle in radians: '))
except ValueError:
return 'ERROR: invalid input!'
# if ValueError *NOT* encountered
if numio != 0.0:
return 1/(numpy.sin(numio))
else:
return 'ERROR: can not divide by 0'
# Scientific modes below this point
def gmol(self):
try:
grams = float(input('Enter grams of material: '))
MW = float(input('Enter MW of material: '))
answer = ext_mod.gmol(grams, MW)
return answer
except ValueError:
return 'ERROR: invalid input!'
def re(self):
try:
kilograms = float(input('Enter mass of object (in kg): '))
answer = ext_mod.re(kilograms)
return answer
except ValueError:
return 'ERROR: invalid input!'