[racket] number->string in the HtDP student languages

From: Nadeem Abdul Hamid (nadeem at acm.org)
Date: Wed Feb 18 08:28:18 EST 2015

And you can use

> (number->string (exact->inexact 0.75))

to get numbers printed in decimal notation. It's slightly
distracting/annoying, but in class I can briefly explain that there is a
distinction between exact and inexact numbers in BSL, and that the former
print as rationals (fractions), and the latter in decimal notation.

--- nadeem


On Wed, Feb 18, 2015 at 7:35 AM, Robby Findler <robby at eecs.northwestern.edu>
wrote:

> In the teaching languages, decimal syntax for numbers does not imply
> inexactness. The only way to get inexact numbers is to call something
> like sqrt or sin. So, there, the number 0.75 and the number 3/4 are
> the same exact number (exact same exact number?).
>
> The printing code is the same (well, it is the same for exact numbers.
> Inexact numbers print with a #i prefix).
>
> Robby
>
>
> On Wed, Feb 18, 2015 at 2:32 AM, Alexis King <lexi.lambda at gmail.com>
> wrote:
> > This is interesting behavior. To explain why it happens, this has
> something
> > to do with how the HtDP languages handle exact/inexact numbers. For
> example,
> > in plain Racket, (exact? 0.75) is #f, but in BSL, it is #true. In BSL,
> > numbers are exact unless otherwise specified. Note that explicitly using
> an
> > exactness prefix in plain Racket, #e0.75, produces 3/4.
> >
> > What’s odd is that the convention for printing numbers in BSL is
> different
> > from the convention used by number->string. BSL, unlike Racket, prints
> exact
> > numbers as decimals. I’d expect that this is to avoid confusion when
> > students type in decimals only to have them printed in large fractions.
> > Unfortunately, it would seem that number->string simply uses the plain
> > Racket implementation, which lets the odd implementations details “leak
> > through” the abstraction.
> >
> > It does feel like poor behavior to me. I’d expect number->string to
> follow
> > the same printing conventions BSL uses.
> >
> > On Feb 18, 2015, at 00:17, Breanndán Ó Nualláin <bon at science.uva.nl>
> wrote:
> >
> > In BSL, I just got caught out by this behaviour:
> >
> > (number->string 0.75)
> >
> >  "3/4"
> >
> > I expected the same behaviour as in the full Racket language:
> >
> > (number->string 0.75)
> >
> >  "0.75"
> >
> > What's the thinking behind this? Wouldn't the decimal notation be more
> > intuitive, particularly in cases like this?
> >
> > (number->string 0.7555555)
> >
> >  "1511111/2000000"
> >
> > And especially in cases like this?
> >
> > (number->string (string->number (number->string pi)))
> >
> >  "3141592653589793/1000000000000000"
> > ____________________
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150218/0703283b/attachment.html>

Posted on the users mailing list.