-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_scieng.py
executable file
·44 lines (34 loc) · 1.07 KB
/
lambda_scieng.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
# Calculates scientific and engineering formats from input value.
import decimal, math
def lambda_handler(event, context):
sci = None
eng = None
input_val = None
sci_forward_exp = None
eng_forward_exp = None
sign = 1
if event.has_key('input_val'):
input_val = event['input_val']
else:
return
input_val = float(input_val)
if input_val == 0.0:
sci = 0.0
eng = 0.0
forward_exp = 1
if input_val < 0.0:
sign = -1
else:
sign = 1
absolute_input_val = float(math.fabs(input_val))
forward_exp = math.floor(math.log10(absolute_input_val))
backward_exp = math.floor(-1 * forward_exp)
sci = sign * (absolute_input_val * math.pow(10, backward_exp))
sci_forward_exp = forward_exp
eng = sci
while forward_exp % 3 != 0:
forward_exp -= 1
backward_exp = -1 * forward_exp
eng = sign * (absolute_input_val * math.pow(10, backward_exp))
eng_forward_exp = forward_exp
return {"statusCode": 200,"headers":{"Access-Control-Allow-Origin":"*","Content-Type":"application/json"},"body":[sci, eng, sci_forward_exp, eng_forward_exp]}