-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex21.py
90 lines (63 loc) · 2.05 KB
/
ex21.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
def add(a, b):
print("Adding %d + %d" % (a, b))
return a + b
def subtract(a, b):
print("Subtracting %d - %d" % (a, b))
return a - b
def multiply(a, b):
print("Multiplying %d X %d" % (a, b))
return a * b
def divide(a, b):
print("Dividing %d / %d" % (a, b))
return a / b
print("Let's do some juggling with maths!")
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print("Age: %d, height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq))
# ==============================
name = 23 + 43.0
print("%d , She's crazy !" % name)
# ==============================
# ======= Anu Doubt ======
print("============================")
first_number = 10.25
second_number = 7.5
print(first_number - second_number)
# error or something that is don't know
x = 20.55
y = 20.91
z = 20.50
print((x + y))
print(x - z)
# by jai
floating_point_values = 0.5
print ("%0.1f" % (floating_point_values))
print ("%0.11f" % (floating_point_values))
print ("%0.01f" % (floating_point_values))
print ("%0.011f" % (floating_point_values))
print("%0.1f" %(x - z))
print("%0.11f" %(x - z))
print("%0.01f" %(x - z))
print(float(x - z))
print()
# Python stores floats with 'bits', and some floats you just can't represent accurately, no matter how many bits of
# precision you have. This is the problem you have here. It's sorta like trying to write 1/3 in decimal with a limited
# amount of decimals places perfectly accurate.
floating_point_values = 0.05
print ("%0.1f" % (floating_point_values))
print ("%0.11f" % (floating_point_values))
print ("%0.01f" % (floating_point_values))
print ("%0.011f" % (floating_point_values))
print ("%0.00f" % (floating_point_values))
print ("%0.001f" % (floating_point_values))
print ("%0.010f" % (floating_point_values))
# here's after all experiments
print ("%0.2f" % (x - z))
print ("%0.3f" % (x - z))
print("============================")
# Puzzle
print("Here is a puzzle !")
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print("That becomes: ", what, ", Can you do it by Hand?")