1

I'm trying to write a program that calculates pi with different accuracies and prints out the number and the time elapsed. I want to print the result with the current accuracy each time. I used print('pi = %*f'%(i, pi)) where i is my current floating point accuracy. This made the program round up the number to the i decimal digit. I'm attaching a picture showing my results running the same algorithm but changing output from:

print (" pi = ", pi, " =with ", i-1, " decimal points accuracy= in: ", (-1)*t, "sec")

to:

print (" pi = %.*f"%(i, pi), " =with ", i-1, " decimal points accuracy= in: ", (-1)*t, "sec")

This is my full code:

import time
accuracy = 4
for i in range(1,accuracy + 1):
    pi = 0
    prevPi = 1
    x = 0
    t = time.time()
    while abs((pi * 4) - prevPi) > 10**((-1)*(i+1)):
    #while x < lim:
        prevPi = pi * 4
        pi += (((-1)**(x))/(1+(2*x)))
        #print(abs((pi * 4) - prevPi))
        x += 1
    pi *= 4
    t -= time.time()
    print (" pi = %.*f"%(i, pi), " =with ", i-1, " decimal points accuracy= in: ", (-1)*t, "sec")

enter image description here

How do I print the number with i decimal digits WITHOUT rounding?

Frank Fourlas
  • 131
  • 1
  • 14
  • 1
    .. And the question is? – Jongware Dec 12 '17 at 22:38
  • How do I make this work without rounding (just diaplaying the first i decimal digits) – Frank Fourlas Dec 12 '17 at 22:42
  • Sorry i got over-exagurated with writing everything else XD – Frank Fourlas Dec 12 '17 at 22:42
  • Floating-point numbers don't work like that. If you want configurable precision, use a type that supports that, like [`decimal.Decimal`](https://docs.python.org/3/library/decimal.html). – user2357112 Dec 12 '17 at 23:38
  • 1
    Also, while round-to-nearest can be unintuitive, and it can feel like it's printing the "wrong" last digit, there's no compelling reason to expect truncation to produce the true value of the corresponding digit of pi, due to natural error in the approximation you're computing. – user2357112 Dec 12 '17 at 23:43
  • 1
    Possible duplicate of [Truncating floats in Python](https://stackoverflow.com/questions/783897/truncating-floats-in-python) – Lycopersicum Dec 13 '17 at 00:21

1 Answers1

1

You could approach your problem by defining function which truncates your number. That function could take two arguments:

  1. number which you want to truncate,
  2. position, at which it would drop all following values.
def truncate(number, position):
    '''Return number with dropped decimal places past specified position.'''
    return number - number%(10**position)

For instance if you would want number 3.14 truncated to 3.1, you should call following:

truncate(3.14, -1)

Also I modified your code so it would be simpler and match PEP 8 coding conventions. Therefore now it has increased variable naming clarity and better code formatting.

#!/usr/bin/env python3
'''Module for different pi accuracies calculation time comparison.'''

from time import time

def truncate(number, position):
    '''Return number with dropped decimal places past specified position.'''
    return number - number%(10**position)

def calculate_pi(accuracy):
    '''Return pi with certain floating point accuracy.'''
    previous_pi = 0 
    current_pi = 4 
    iterator = 1 
    while abs(current_pi - previous_pi) > 10 ** (accuracy-1):
        previous_pi = current_pi
        current_pi += 4 * ((-1)**iterator) / (1+(2*iterator))
        iterator += 1
    return truncate(current_pi, accuracy)

def calculation_speed_comparison(max_accuracy):
    '''Print comparison of different accuracy pi calculation time.'''
    for current_accuracy in range(max_accuracy+1):
        calculation_time = time()
        current_pi = calculate_pi(-current_accuracy)
        calculation_time -= time()
        print('pi = {} with {} decimal points accuracy in {} seconds.'.format(
            current_pi, current_accuracy, -calculation_time))

calculation_speed_comparison(4)

Output to this code remains very similar to original one:

pi = 3.0 with 0 decimal points accuracy in 3.266334533691406e-05 seconds.
pi = 3.1 with 1 decimal points accuracy in 0.00016045570373535156 seconds.
pi = 3.14 with 2 decimal points accuracy in 0.0014882087707519531 seconds.
pi = 3.141 with 3 decimal points accuracy in 0.01430201530456543 seconds.
pi = 3.1415 with 4 decimal points accuracy in 0.1466822624206543 seconds.
Lycopersicum
  • 529
  • 1
  • 6
  • 17