[m-rev.] for review: fix float_to_string so that roundtripping works
Ralph Becket
rafe at cs.mu.OZ.AU
Thu Nov 21 11:57:07 AEDT 2002
Fergus Henderson, Thursday, 21 November 2002:
> On 20-Nov-2002, Peter Ross <pro at missioncriticalit.com> wrote:
> > +++ library/string.m 20 Nov 2002 16:42:19 -0000
> > @@ -1826,45 +1826,21 @@
> > :- pragma foreign_proc("C",
> > string__float_to_string(Flt::in, Str::uo),
> > [will_not_call_mercury, promise_pure, thread_safe], "{
> > + char buf[ML_SPRINTF_FLOAT_BUF_SIZE];
> > + ML_sprintf_float(buf, Flt);
> > + MR_make_aligned_string(Str, buf);
>
> That needs to be MR_make_aligned_string_copy().
>
> > +:- pragma foreign_code(c, "
> > +void
> > +ML_sprintf_float(char *buf, MR_Float f)
> > +{
> > + MR_Float round;
> > + int i = ML_MIN_PRECISION;
> > +
> > + do {
> > + sprintf(buf, ""%#.*g"", i, f);
> > + if (i >= ML_MAX_PRECISION) {
> > + /*
> > + ** This should be sufficient precision to
> > + ** round-trip any value. Don't bother checking
> > + ** whether it can actually be round-tripped,
> > + ** since if it can't, this is a bug in the C
> > + ** implementation.
> > + */
> > + break;
> > + }
> > + sscanf(buf, ML_FMT, &round);
> > + i++;
> > + } while (round != f);
> > +}
> > +").
Small point, but isn't a for loop more appropriate here?
for(i = ML_MIN_PRECISION; i <= ML_MAX_PRECISION; i++) {
sprintf(buf, "%#.*g", i, f);
sscanf(buf, ML_FMT, &round);
if(round == f) {
break;
}
}
- Ralph
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list