----- Original Message -----
From: "Ladislav Mecir" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, December 30, 2005 8:40 PM
Subject: [REBOL] Re: REBOL Newbie: Floating Point Rounding / Formatting
Function ?
Ladislav,
>
> Anthony Borla napsal(a):
>
> >Greetings,
> >
> >I'm currently 'doodling' with REBOL/Core [after all, what
> > else would one be doing over this Holiday Season ;) ?], and
> > am somewhat perplexed by the seeming lack of a floating
> > point rounding / formatting function, something along the lines
> > of Scheme's 'format', or C's 'printf' function.
> >
> Regarding rounding: there is the ROUND function.
>
Sure, I tried that, but the result it gives is an integer. For example,
doing:
round 123.456
gives an integer result:
123
At first I thought /scale could be used to achieve such rounding, but all my
attempts at doing so have failed. For example:
round 123.456 /scale 2
gives an integer result of:
2
I've obviously misunderstood how /scale is used.
What I'm after is a function that rounds to specified decimal places. For
example:
123.456
rounded to 2 decimal places is:
123.46
and to 1 decimal place is:
123.5
and so on. Such a function is - in my experience with other languages -
usually a built-in facility, a core part of the language.
Please don't think I'm being critical of REBOL: I'm not. I'd simply like to
know how such a task is accomplished in REBOL because I don't know, and have
not had much luck so far in finding out [as I mentioned in the subject line,
I'm a newbie :) !].
>
> As to the formatting:
> there are functions doing floating point formatting, but the
> fact is, that there is no standard function used by everyone
> yet.
>
Oh, ok. I certainly wasn't aware of that. Thank you for the information.
>
> I think, that it is caused by the fact, that nobody has
> proposed something that could be found both practical
> and simple. Have you got any idea how should such a
> function look? (i.e. I am not proposing you to implement
> such a function, rather to design the interface - how should
> the function be called etc.)
>
I can't say I've given any thought to this. Off-hand, though, a function
like LISP / Scheme's [also Prolog] 'format' function would probably [I would
think] be useful. This function is passed a 'format string' [basically a
mini-language describing how output is to appear i.e. describe required
length, justification, leading / trailing characters, decimal positions
etc], and the data to which that format is applied.
For example, in Prolog, the following would print the floating point value
of 'Energy' rounded to 9 decimal places followed by a newline:
...
format('~9f~N', [Energy]),
...
>From the little I know of REBOL I'd imagine similar functionality could be
achieved via the use of 'refinements' [pardon me if my terminology or ideas
are incorrect]. Hopefully, though, you understand [at least roughly] what I
mean.
I realise that REBOL offers a rich set of functions for performing word /
string manipulation, and that such rounding / formatting tasks can be
performed by creating custom functions. I would imagine, though, that if
such is supplied as a built-in function, the language convenience factor
increases enormously.
Same goes, I think, for needing to interface to a C library for such
functionality. For example, the C library's, 'printf' family of functions
could be used for this task. But, again, I'd say a more REBOL-like built-in
function may well be preferable [it could, perhaps use 'printf' to perform
the formatting services whilst offering a more convenient user interface ?].
Actually, I didn't think simply asking about a rounding function could turn
into such an interesting discussion :) !
Cheers,
Anthony Borla
--
To unsubscribe from the list, just send an email to
lists at rebol.com with unsubscribe as the subject.