2015-04-04 17:05:30 -04:00
|
|
|
# test basic float capabilities
|
|
|
|
|
2015-04-04 18:16:22 -04:00
|
|
|
# literals
|
|
|
|
print(.12)
|
|
|
|
print(1.)
|
|
|
|
print(1.2)
|
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# float construction
|
|
|
|
print(float(1.2))
|
2015-04-22 11:51:29 -04:00
|
|
|
print(float("1.2"))
|
|
|
|
print(float("+1"))
|
|
|
|
print(float("1e1"))
|
|
|
|
print(float("1e+1"))
|
|
|
|
print(float("1e-1"))
|
|
|
|
print(float("inf"))
|
2015-05-16 04:54:19 -04:00
|
|
|
print(float("-inf"))
|
2015-04-22 11:51:29 -04:00
|
|
|
print(float("INF"))
|
|
|
|
print(float("infinity"))
|
|
|
|
print(float("INFINITY"))
|
|
|
|
print(float("nan"))
|
|
|
|
print(float("NaN"))
|
|
|
|
try:
|
|
|
|
float("1e+")
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
|
|
|
try:
|
|
|
|
float("1z")
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
2015-04-04 17:05:30 -04:00
|
|
|
|
|
|
|
# unary operators
|
|
|
|
print(bool(0.0))
|
|
|
|
print(bool(1.2))
|
|
|
|
print(+(1.2))
|
|
|
|
print(-(1.2))
|
|
|
|
|
|
|
|
# division of integers
|
2013-12-29 17:34:42 -05:00
|
|
|
x = 1 / 2
|
|
|
|
print(x)
|
2014-03-30 19:20:00 -04:00
|
|
|
|
2015-03-12 18:47:44 -04:00
|
|
|
# /= operator
|
|
|
|
a = 1
|
|
|
|
a /= 2
|
|
|
|
print(a)
|
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# floor division
|
2014-03-30 19:20:00 -04:00
|
|
|
print(1.0 // 2)
|
|
|
|
print(2.0 // 2)
|
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
# comparison
|
|
|
|
print(1.2 <= 3.4)
|
|
|
|
print(1.2 <= -3.4)
|
|
|
|
print(1.2 >= 3.4)
|
|
|
|
print(1.2 >= -3.4)
|
|
|
|
|
2014-03-30 19:20:00 -04:00
|
|
|
try:
|
|
|
|
1.0 / 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
1.0 // 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
2015-02-07 20:57:40 -05:00
|
|
|
|
2015-04-04 17:05:30 -04:00
|
|
|
try:
|
|
|
|
1.2 % 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
|
|
|
# unsupported unary ops
|
|
|
|
|
|
|
|
try:
|
|
|
|
~1.2
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
1.2 in 3.4
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2015-03-25 19:10:09 -04:00
|
|
|
# can't convert list to float
|
|
|
|
try:
|
|
|
|
float([])
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2015-02-07 20:57:40 -05:00
|
|
|
# test constant float with more than 255 chars
|
|
|
|
x = 1.84728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189
|
|
|
|
print("%.5f" % x)
|