2015-04-04 21:05:30 +00:00
|
|
|
# test basic float capabilities
|
|
|
|
|
2015-04-04 22:16:22 +00:00
|
|
|
# literals
|
2020-03-23 02:26:08 +00:00
|
|
|
print(0.12)
|
|
|
|
print(1.0)
|
2015-04-04 22:16:22 +00:00
|
|
|
print(1.2)
|
2015-09-07 16:33:44 +00:00
|
|
|
print(0e0)
|
2020-03-23 02:26:08 +00:00
|
|
|
print(0e0)
|
2015-09-07 16:33:44 +00:00
|
|
|
print(0e-0)
|
2015-04-04 22:16:22 +00:00
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
# float construction
|
|
|
|
print(float(1.2))
|
2015-04-22 15:51:29 +00: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 08:54:19 +00:00
|
|
|
print(float("-inf"))
|
2015-04-22 15:51:29 +00:00
|
|
|
print(float("INF"))
|
|
|
|
print(float("infinity"))
|
|
|
|
print(float("INFINITY"))
|
|
|
|
print(float("nan"))
|
2017-10-04 12:15:55 +00:00
|
|
|
print(float("-nan"))
|
2015-04-22 15:51:29 +00:00
|
|
|
print(float("NaN"))
|
2016-09-27 05:44:56 +00:00
|
|
|
try:
|
|
|
|
float("")
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
2015-04-22 15:51:29 +00:00
|
|
|
try:
|
|
|
|
float("1e+")
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
|
|
|
try:
|
|
|
|
float("1z")
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
2015-04-04 21:05:30 +00:00
|
|
|
|
2017-11-21 04:01:38 +00:00
|
|
|
# construct from something with the buffer protocol
|
|
|
|
print(float(b"1.2"))
|
|
|
|
print(float(bytearray(b"3.4")))
|
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
# unary operators
|
|
|
|
print(bool(0.0))
|
|
|
|
print(bool(1.2))
|
|
|
|
print(+(1.2))
|
|
|
|
print(-(1.2))
|
|
|
|
|
|
|
|
# division of integers
|
2013-12-29 22:34:42 +00:00
|
|
|
x = 1 / 2
|
|
|
|
print(x)
|
2014-03-30 23:20:00 +00:00
|
|
|
|
2015-03-12 22:47:44 +00:00
|
|
|
# /= operator
|
|
|
|
a = 1
|
|
|
|
a /= 2
|
|
|
|
print(a)
|
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
# floor division
|
2014-03-30 23:20:00 +00:00
|
|
|
print(1.0 // 2)
|
|
|
|
print(2.0 // 2)
|
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
# comparison
|
|
|
|
print(1.2 <= 3.4)
|
|
|
|
print(1.2 <= -3.4)
|
|
|
|
print(1.2 >= 3.4)
|
|
|
|
print(1.2 >= -3.4)
|
2020-02-10 11:22:12 +00:00
|
|
|
print(0.0 == False, 1.0 == True)
|
|
|
|
print(False == 0.0, True == 1.0)
|
2015-04-04 21:05:30 +00:00
|
|
|
|
2017-09-04 04:16:27 +00:00
|
|
|
# comparison of nan is special
|
2020-03-23 02:26:08 +00:00
|
|
|
nan = float("nan")
|
2017-09-04 04:16:27 +00:00
|
|
|
print(nan == 1.2)
|
|
|
|
print(nan == nan)
|
|
|
|
|
2014-03-30 23:20:00 +00:00
|
|
|
try:
|
|
|
|
1.0 / 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
1.0 // 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
2015-02-08 01:57:40 +00:00
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
try:
|
|
|
|
1.2 % 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
2017-02-02 13:04:13 +00:00
|
|
|
try:
|
2022-02-02 05:11:25 +00:00
|
|
|
0.0**-1
|
2017-02-02 13:04:13 +00:00
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
2015-04-04 21:05:30 +00:00
|
|
|
# unsupported unary ops
|
|
|
|
|
|
|
|
try:
|
|
|
|
~1.2
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
1.2 in 3.4
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2016-12-21 03:47:02 +00:00
|
|
|
# small int on LHS, float on RHS, unsupported op
|
|
|
|
try:
|
|
|
|
print(1 | 1.0)
|
|
|
|
except TypeError:
|
2020-03-23 02:26:08 +00:00
|
|
|
print("TypeError")
|
2016-12-21 03:47:02 +00:00
|
|
|
|
2015-03-25 23:10:09 +00:00
|
|
|
# can't convert list to float
|
|
|
|
try:
|
|
|
|
float([])
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
# test constant float with more than 255 chars
|
|
|
|
x = 1.84728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189
|
|
|
|
print("%.5f" % x)
|