Post No. 12.25i: And Now Let’s Explore Irrational Numbers

Hello everybody,

Michael here, and today for my 150th post I thought I’d do something a little different. This time, something with a broader scope-irrational nnumbers in programming. This isn’t my first broad post (after all, I did Colors in Programming in November 2021) but hey, what better time for a broad post than my 150th overall post (did anyone catch the little detail in the post title hinting that this is the 150th post?).

But first, a little bit on irrational numbers

What are irrational numbers? You’ve likely heard of them if you’ve at least taken pre-algebra but for those who don’t know or don’t remember, irrational numbers arre simply numbers that can’t be expressed as fractions.

What would a rational number look like? Well, think of the 10 cardinal digits from 0-9, any fraction like 1/2 or 3/4 (yes, even improper fractions count as rational numbers), and any decimal (regardless of how many decimal points it carries on for).

Now, what would an irrational number look like? I went over one of them in an earlier post-e, also known as Euler’s Number-2.71828 (check out R Lesson 30: Logarithms for more information on Euler’s number). Some other irrational numbers include imaginary numbers (like 3+2i), the square roots of all negative numbers and positive prime numbers (which in turn are imaginary), the golden ratio, and of course, PI.

Imaginary numbers are their own special type of irrational numbers-and it’s important to remember that while all imaginary numbers are irrational, not all irrational numbers are imaginary. Imaginary numbers always contains an imaginary unit i (though the letter used to represent that imaginary unity can vary-more on that later), which equals the square root of -1.

If a number has a “real” and “imaginary” part, then it is referred to as a complex number. Take the complex number 3+2i. The 3 would be the “real” part and the 2i would be the “imaginary” part. The same logic would apply for a number like 6i, which, even though it only has a single part, is still consered a complex number since it has a “real” part (a hidden 0) and an imaginary part (6i).

  • For those that haven’t heard of the golden ratio before, it’s a mathematical constant (just like PI and e) that equals approximately 1.61 that is often considered as an “aesthethically pleasing” number as it can be found in fields such as art, nature, and photography (just to name a few).

Imaginary numbers in programming

Now that we’ve discussed the basics of what irrational numbers are all about, let’s explore how they’re used in programming.

First off, let’s see how imaginary numbers work in programming! Here’s an example of simple arthemic with imaginary numbers in Python:

print(3+5j*2)
(3+10j)

As you can see, Python is capabale of simple arithmetic with imaginary numbers (and is likely capabale of far more complex mathematics too). Also notice how Python uses a j to represent an imaginary unit instead of an i. Watch what happens when you try to use an i in this expression:

print(3+5i*2)

File "<ipython-input-5-f1daeb22835b>", line 1
    print(3+5i*2)
             ^
SyntaxError: invalid syntax

Trying to use i for the imaganiary unit will give you an error in this expression.

Now let’s take a look at how imaginary numbers are used in other programming languages! Here are two examples of expressions with imaginary numbers in R:

(3i+4)*(2i-2)
[1] -14+2i

(7i+3)-2
[1] 1+7i

As you can see, R-just like Python-performs simple imaginary number arithmetic quite well. One interesting quirk with R is that, no matter how I list the imaginary number in my expression (whether in the form of real part+imaginary part or imaginary part+real part), the output always displays in the form of real part+imaginary part.

Other irrational numbers in programming

Now that we briefly explored imaginary numbers in programming, let’s turn our attention to other irrational numbers!

Here’s an example in Python showing how to obtain irrational, non-imaginary numbers-e, the golden ratio, and pi.

from scipy import constants

from scipy import constants

print(constants.golden)
print(constants.pi)
print(constants.e)

1.618033988749895
3.141592653589793
1.602176634e-19

In case you ever wanted to use pi, the golden ratio (denoted by scipy.constants.golden) or e in any Python calculation, you now know the easy way to access these irrational numbers-just use the from scipy import constants line to access the scipy constants package and then use the line scipy.constants.[number you wish to use] to access the irrational number.

If that seems cool, here are some other constants the scipy.constants package contains (and there are far too many to mention for this entry):

from scipy import constants

print(constants.year)
print(constants.day)
print(constants.hour)

31536000.0
86400.0
3600.0

These are just a few of the time constants that the scipy.constants module contains-and in case your wondering why year gave you such a large number, it’s because these time constants are stored in seconds (yes, a year contains 31,536,000 seconds). The same logic applies as to why day and hour give you seemingly odd large numbers.

  • In case you’re wondering what the SciPy package is, first of all, it stands for scientific Python. The SciPy package is built upon the numpy package and contains various helpful mathematical and scientific functions and modules (like the constants module we just discussed).
  • Let me know if you all would like to see a SciPy series of lessons!

Now that we know how to obtain these irrational, non-imaginary numbers in Python, let’s see them used in computations!

print(constants.golden*3)
print(constants.pi**2)
print(constants.e/100)

4.854101966249685
9.869604401089358
1.6021766339999998e-21

Not gonna lie, Python’s computations of these mathematical constants work like a charm!

Now to explore these irrational, non-imaginary numbers in another programming language! I know, let’s try Java!

import java.lang.Math;

public class Numbers {


    public static void main(String[] args) {
        long product = (long) (Math.E*6);
        long sum = (long) (Math.PI+12);
        
        System.out.println(product);
        System.out.println(Math.PI);
        System.out.println(Math.E);
        System.out.println(sum);
        
    }
    
}

16
3.141592653589793
2.718281828459045
15

As you can see, Java can be a bit trickier than R or Python when it comes to working with irrational numbers (after all, there are more imports necessary-plus the need to cast any expression with these irrational numbers into type long)!

Another thing you’ll notice with Java is that in it’s Math class, PI and E are the only irrational number constants built-in to the class. The golden ratio isn’t built in, but you can easily define it. Here’s how you’d do so in Java:

long golden = (long) (1+Math.sqrt(5)/2)

And here’s what that number would look like in the output:

2

For some odd reason, if I use a long for my golden ratio in Java, I get a 2 in return (even though the approximation of the golden ratio equals 1.61).

Thanks for reading these past 12.25i posts. Here’s to the next 12.25i!

Michael

Leave a Reply