More concretely, and continuing the arbitrary precision thread, one might
think Python, with its clever duck typing, could take either floating
point, or standard library Decimals, through precisely the same algorithm.

That's so in some cases, but when we get to powering, one can't use the
built-in pow( ) function with pow(Decimal, float), as a workaround for
math.sqrt.  sqrt(float) is fine of course, but you need Decimal.sqrt()
whereas float.sqrt() is a syntax error.  The algorithm has to "know in
advance" what syntax to use, meaning we stray into type detection (what
you'd think we might avoid, given numbers are numbers).

I ended up with something kinda kludgey like this:

def rt2(n):
if "sqrt" in dir(n):
return n.sqrt()
else:
return sqrt(n)
A simple type check would be faster, by why not go all the way and let any
object with a sqrt method use that instead of math.sqrt?

Then I initialize my variables like this:

if high:
a = Decimal('1')
seven = Decimal('7')
five = Decimal('5')
three = Decimal('3')
else:
a = 1
seven = 7
five = 5
three = 3

EF = a * rt2(seven - 3 * rt2(five))
EH = a * (3 - rt2(five))/2
EG = a * EF / 2
FH = a * (rt2(five) - 1)/2
HG = a * EG
GF = a * rt2(three) * EG

Corresponding pictures (if curious):
https://mybizmo.blogspot.com/2020/01/quaker-curriculum-american-literature.html

That way I get my edge lengths as either floating point or decimal,
depending on whether high (for "high precision") is set to True.

Even then though, when I feed these six edges of a tetrahedron to the
volume formula (six edges --> volume), I come up against needing a 2nd root
and end up duplicating the rt2 definition inside the Tetrahedron class as
_rt2.  Details here:

https://repl.it/@kurner/tetravolumes

Mathematica (Wolfram Language) makes all this easier.  But that's not a
workaday coding language most people use.

Given how most other computer languages work, I think introducing IEEE 754
floating points *in contrast with* an arbitrary precision type, is
reassuring to students new to computers.  Why not make the most of Decimal
and/or 3rd party gmpy2?

Show them they really *can* use pi or phi to a thousand places (you'd think
computers would be able to do that).  That's pedagogically important in my
book.  gmpy2 has native trig and allows complex numbers.

Making floating points do all the work and calling those "real numbers"
only adds to the pure math | engineering divide I'd like to see bridged.
Using floating points only leaves the wrong impression that computers are
actually bad at doing ordinary math textbook computations.

Kirby
_______________________________________________
Edu-sig mailing list -- edu-sig@python.org
To unsubscribe send an email to edu-sig-le...@python.org
https://mail.python.org/mailman3/lists/edu-sig.python.org/

Reply via email to