Data type of π ??🤔🤔 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Data type of π ??🤔🤔

In which data type does π come in ? Because it can be written in both rational and irrational forms ?

5th Jun 2023, 4:41 PM
Aryan Raj Patel
Aryan Raj Patel - avatar
4 Answers
+ 1
Actually pi is not a rational number, we commonly use 22/7 or 3.14 for approximate but it can't be replaced pi To use pi in python, we use 'math' module Code -- from math import pi print(pi) The syntax is same for numpy or script Code-- From numpy import pi Print(pi) Code-- From scipy import pi Print(pi) It will show you at float type as it is an irrational number (non repeating non terminating) but it stops after 8 or 16 places as there is a limit set there that a float data type can store only certain bytes . This also applies to 'e' and other irrational numbers. In these three modules, the value of pi is same. Exactly equals to 3.141592653589793
7th Jun 2023, 7:38 AM
Harshit Singh
Harshit Singh - avatar
+ 8
π has never been a rational number. Its precise value cannot be expressed in decimal or binary number system, as computers work. So we can only approximate it. The pi available in the math library, is a float type. You can check like this: import math print(math.pi) print(type(math.pi)) There are also other libraries that give a representation of pi. numpy and scipy use the same value as the math library. However, sympy uses a different value. https://stackoverflow.com/questions/12645547/should-i-use-scipy-pi-numpy-pi-or-math-pi
5th Jun 2023, 4:59 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Aryan Raj Patel Value of π come in float datatype
6th Jun 2023, 2:51 PM
Alhaaz
Alhaaz - avatar
+ 1
Oh thanks!
6th Jun 2023, 3:55 AM
Aryan Raj Patel
Aryan Raj Patel - avatar