[Tutor] the binary math "wall"
Lowell Tackett
lowelltackett at yahoo.com
Sun Apr 25 20:10:17 CEST 2010
>From the virtual desk of Lowell Tackett
--- On Tue, 4/20/10, Steven D'Aprano <steve at pearwood.info> wrote:
> From: Steven D'Aprano <steve at pearwood.info>
> Subject: Re: [Tutor] the binary math "wall"
> To: tutor at python.org
> Date: Tuesday, April 20, 2010, 7:39 PM
> On Wed, 21 Apr 2010 02:58:06 am
> Lowell Tackett wrote:
> > I'm running headlong into the dilemma of binary math
> representation,
> with game-ending consequences, e.g.:
> > >>> 0.15
> But if you really need D.MMSS floats, then something like
> this should be
> a good start.:
>
> def dms2deg(f):
> """Convert a floating point number formatted
> as D.MMSS
> into degrees.
> """
> mmss, d = math.modf(f)
> assert d == int(f)
> if mmss >= 0.60:
> raise ValueError(
> 'bad fractional part, expected
> < .60 but got %f' % mmss)
> mmss *= 100
> m = round(mmss)
> if m >= 60:
> raise ValueError('bad minutes,
> expected < 60 but got %d' % m)
> s = round((mmss - m)*100, 8)
> if not 0 <= s < 60.0:
> raise ValueError('bad seconds,
> expected < 60.0 but got %f' % s)
> return d + m/60.0 + s/3600.0
>
>
> >>> dms2deg(18.15)
> 18.25
> >>> dms2deg(18.1515)
> 18.254166666666666
>
>
Haven't gone away...I'm having a blast dissecting (parsing[?]-is that the right word?) your script snippet! Got a big pot of coffee fueling the effort.
>
> Note though that this still fails with some valid input. I
> will leave
> fixing it as an exercise (or I might work on it later, time
>
> permitting).
Got the hint...I'm gonna pick up this challenge. This effort is taking me in endless tangents (all productive)-I'll be back sometime [soon] with my efforts and results. Thanks for your [own] time and effort.
>
>
> --
> Steven D'Aprano
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list