[Tutor] Python problem

Vakhtang Kvaratskhelia va_kvaratskhelia at cu.edu.ge
Mon Feb 22 11:47:06 EST 2021


Thank you very much for the reply.

I understand those comments, however what i don't understand is how can you
use integer division "//" with the bisection search code? The "/" division
works just fine but the "//" doesn't work with the bisection search code.

Is it possible that the hint is wrong? Or maybe integer division doesn't
mean "//"

On Sat, Feb 20, 2021 at 9:31 PM Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:

> On Sat, 20 Feb 2021 15:27:01 +0400, Vakhtang Kvaratskhelia
> <va_kvaratskhelia at cu.edu.ge> declaimed the following:
>
> >
> >I want to use this hint : "Because we are searching for a value that is in
> >principle a float, we are going to limit ourselves to two decimals of
> >accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal – but we
> >are not going to worry about the difference between 7.041% and 7.039%).
> >This means we can search for an integer between 0 and 10000 (using integer
> >division), and then convert it to a decimal percentage (using float
> >division) to use when we are calculating the current_savings after 36
> >months. By using this range, there are only a finite number of numbers
> that
> >we are searching over, as opposed to the infinite number of decimals
> >between 0 and 1. This range will help prevent infinite loops. The reason
> we
> >use 0 to 10000 is to account for two additional decimal places in the
> range
> >0% to 100%. Your code should print out a decimal (e.g. 0.0704 for 7.04%)."
> >but i am not able to.
>
>         Bottom up. Python floats are double precision -- approximately 15
> significant digits -- ALWAYS.
>
>         If you need specific formatting for output, you have to provide the
> correct formatting string when outputting the result. Your code is just
> outputting a sequence of a string and a numeric with no formatting codes --
> so naturally Python is going to display as many significant decimals as it
> can...
>
> >>> import math
> >>> math.pi
> 3.141592653589793
> >>> "%7.4f" % math.pi
> ' 3.1416'
> >>>
>
>         I'm not so certain about that justification for using integers and
> then
> converting to float for output... While real world may have an infinite
> range between 0.0 and 1.0, computers do not. Furthermore, if you are using
> an epsilon to define when two values are "close enough" you will never be
> computing in increments of less than that epsilon.
>
>         abs(a-b)<0.00005
>
>         Of course, going further from the track... If you are discussing
> financial values, using the Decimal module configured for the desired
> resolution is probably recommended.
>
>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed at ix.netcom.com
> http://wlfraed.microdiversity.freeddns.org/
>
> _______________________________________________
> 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