Why are two values different for the same equation?

I decided to get creative with the area calculator and came up with my own two versions of it. One version follows the course somewhat closely while the other changes it up completely.

I’m getting two different values for the same equation when I run it. Why is that?

AreaCalculator.py (Follows the course somewhat closely)

"""
This program allows you to calculate the area
of a chosen shape. (Area Calculator)
(Takes a lot of work to extend and is semi-difficult to read)
"""


# Vars
pi = 3.14159

print 'Area calculator is starting.'
print 'The value of pi is %f. \n' % (pi)

def calc():
  shape = raw_input('Circle, Triangle, or Exit: ')
  
  if shape[0].lower() == 'c': # Circle
    print 'You chose to calculate the area of a circle.'
    r = float(raw_input('Radius: '))
    area = pi * r**2
    print "\nThe area of your circle is: %f" % (area)
    
  elif shape[0].lower() == 't': # Triangle
    print 'You chose to calculate the area of a triangle.'
    b = float(raw_input('Base: '))
    h = float(raw_input('Height: '))
    area = 0.5 * b * h
    print "\nThe area of your triangle is: %f" % (area)
    
  elif shape[0].lower() == 'e': # Exit
    exit(1)
    
  else: # Foolishness
    print 'Please type either "Circle", "C", "Triangle", or "T".\nDon\'t be a fool. \n\n'
    calc()
    
calc()

When using the area calculator for a circle and using 6 as the radius, I get 113.097240
When using the area calculator for a triangle and using 6 as the base and height, I get 18.000000

AreaCalculator2.py (Changes everything)

"""
Revision of calculator using 
functions the right way.
(Can be extended easily to have more math operations. Easy to read.)
"""
# Vars
pi = 3.14159

# Menu
def menu():
  print 'Welcome to AreaCalculator2.py!'  
  print 'Would you like to find the area of a Circle(1) or a Triangle(2)?\n'
  return int(raw_input('Circle(1), Triangle(2), or Quit(3)? '))

# Circle computation
def circle(r):
  print '\nThe area of your circle is', pi * r**2
  exit()

# Triangle computation
def triangle(b,h):
  print 'The area of your triangle is', 0.5 * b * h
  exit()
  
# Call everything
loop = 1
choice = 0

while loop == 1:
  choice = menu()
  
  if choice == 1:
      print 'The value of pi is %f' % (pi)
      circle(float(raw_input('Radius: ')))
      
  elif choice == 2:
      triangle(float(raw_input('Base: ')),(float(raw_input('Height: '))))
    
  elif choice == 3:
      loop = 0

print 'Thanks for using me!' 

When using the area calculator for a circle and using 6 as the radius, I get 113.09724
When using the area calculator for a triangle and using 6 as the base and height, I get 18.0

I’m not sure why it’s truncating the extra 0s at the end when I’m using float(raw_input("x: ")) in both instances. Why does this happen?

1 Like

Hi @appledash48,

Both of your calculators compute the same values when they perform the mathematics. The difference is how you are formatting the output. By default, the %f format specifier rounds the corresponding value to six decimal places, even if the result contains trailing zeroes. You can specify the number of places that are output. For example, the following will provide four decimal places:

print("%0.4f" % (pi))

There are newer options available for formatting, one of which is described in detail here: PyFormat: Using % and .format() for great good!. This will provide five decimal places:

print("The value of pi is approximately {:0.5f}.".format(pi))

The most recent versions of Python offer formatted string literals. See PEP 498: Formatted string literals.

If you would like to increase the precision of your calculators, import the math module:

import math

Then, you can use math.pi to access the value of π to additional decimal places. See math.pi.

2 Likes

Ahh, I see! I didn’t realize that %f rounds to 6 decimal places by default. I will certainly be reading up on .format() and the math module now thanks to your suggestions. I appreciate your help! :smile:

2 Likes