[Tutor] My function correct_num()
Alex Kleider
alexkleider at gmail.com
Thu Mar 30 19:39:25 EDT 2023
String formatting might be a better way to deal with your issue.
On Thu, Mar 30, 2023, 4:36 PM Goran Ikac <goranikac65 at gmail.com> wrote:
> Hi, I wish a nice day to every pythonist out there!
> I'm a newbie, still learning the basics. For an exercise, I wrote a
> function *correct_num() *to avoid ridiculous presentation of some numeric
> calculations in Python (e.g. *print(.1 + .1 + .1) *outputs
> *0.30000000000000004*):
>
> # written by ike 2023
>
> def correct_num(num: float) -> float:
> """
> The function takes any number as the only argument.
> If the number is an integer, or if it is a float with up to *six *
> decimal
> digits, the function returns the same number.
> If the number is a float with more than *six *decimal digits,
> the function returns that number with up to one digit less precision.
> That way, it corrects the errors made by the computer's numerical
> calculations.
>
> >>> .1 + .1 + .1
> 0.30000000000000004
> >>> correct_num(.1 + .1 + .1)
> 0.3
> >>> 24.95 - 9.98
> 14.969999999999999
> >>> correct_num(24.95 - 9.98)
> 14.97
> >>> correct_num(123)
> 123
> >>> correct_num(123.0)
> 123.0
> >>> correct_num(-123.0)
> -123.0
> >>> correct_num(0.123456)
> 0.123456
> >>> correct_num(0.1234567)
> 0.123457
> >>> correct_num(0.123456789)
> 0.12345679
> >>> correct_num(-0.123456789)
> -0.12345679
> >>> correct_num(5.12345678e-5)
> 5.1e-05
> >>> correct_num(5.17345678e-5)
> 5.2e-05
> >>> correct_num(5.173e-6)
> 5e-06
> >>> correct_num(5.173e-7)
> 1e-06
> >>> >>> correct_num(5.173e-8)
> 0.0
>
> """
>
> if num == int(num): # If the number is an integer,
> return num # return the same number.
>
> numstr = str(num)
>
> if 'e' in numstr: # If num is written in scientific
> notation
> numstr = '{:.*7*f}'.format(num)
>
> # print('numstring ==', numstr) # This is a control line.
>
> if '.' in numstr: # If the numstr represents a float,
> # determine the number of num's decimal digits
> dec_digits = len(numstr[(numstr.index('.') + 1):])
> # If the limitation in how computers do arithmetic doesn't affect
> num:
> if dec_digits < *7*:
> return num # return the same number.
>
> return round(num, dec_digits - 1)
>
>
> Now, I'm happy with the function's work, but I don't know what number of
> decimal digits to leave alone, e.g. what number of decimal digits are
> certainly OK). I've decided it to be *six *decimal digits:
> if dec_digits < *7*:
> return num # return the same number.
> but it was by pure intuition. Does anybody know what is the right number of
> decimal digits to leave as they were returned by Python numeric
> calculations?
> And, of course, I'd be thankful for any comment about my code.
> Also, can anybody, please, correct my English in the docstring?
> Watch out! I'm riding to catch up with you, big boys and girls.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list