"Alex Krycek" <······@sympatico.ca> writes:
> Is there anyway I can get the compiler to round out a number like :
>
> ex. "=> 2"
>
> instead of
>
> " =>21523361 / 10761680" ??
how about ROUND?
(round (/ 21523361 10761680)) => 2 1/10761680
you may also want to look at COERCE to force something to DOUBLE-FLOAT
and such like.
--
J o h a n K u l l s t a m
[········@ne.mediaone.net]
Don't Fear the Penguin!
> how about ROUND?
> (round (/ 21523361 10761680)) => 2 1/10761680
Depending on your compiler's smartness, you probably want to say:
(round 21523361 10761680) rather than the more obvious (round (/
..)). ROUND is defined tp takle an optional divisor, and may be able
to avoid consing rationals. I guess a smart compiler could spot this
case and compile identical code, but I don't know of any that do.
--tim
Alex Krycek wrote:
>
> Is there anyway I can get the compiler to round out a number like :
>
> ex. "=> 2"
>
> instead of
>
> " =>21523361 / 10761680" ??
A Sneaky hack is to multiply it by a decimal number (i.e. 1.0), which
will convert it to a 12345.6789 decimal notation. Then you can (trunc
..) or (round ..) it to the nearest decimal number to remove unwanted
precision.
Of course, not all environments will necessarily convert to decimal just
because you multiplied by 1.0 .. but it shoudl work sometimes.
CU
Dobes
Dobes Vandermeer <·····@mindless.com> writes:
> Alex Krycek wrote:
> >
> > Is there anyway I can get the compiler to round out a number like :
> >
> > ex. "=> 2"
> >
> > instead of
> >
> > " =>21523361 / 10761680" ??
>
> A Sneaky hack is to multiply it by a decimal number (i.e. 1.0),
In Common Lisp, as in Mathematics, the concept of something being
"decimal" is an artifact of the reader or printer. You're perhaps
confusing decimal with the type FLOAT. 13 is a decimal number
or an octal number or a hex number. 13.0 is a FLOAT, which happens
to use decimal digits and a decimal point to express itself.
> which
> will convert it to a 12345.6789 decimal notation.
Numbers are not stored in a notation. They are stored in a representation.
A notation is like "octal notation" which is one of several ways you
can print the number which is the result of adding 5 and 5. Printed
in octal notation, that numbers is 12. Printed in decimal notation, it
is 10. But it is represented as the same number in either case, and
multiplication (whether by 1.0 or anything else) does not change the
notation except incidentally as a side-effect of having changed the type.
By the way, to change a rational to a float, just use FLOAT.
Don't multiply by 1.0.
> Then you can (trunc
> ..) or (round ..) it to the nearest decimal number to remove unwanted
> precision.
You can do these directly on rationals.
> Of course, not all environments will necessarily convert to decimal just
> because you multiplied by 1.0 .. but it shoudl work sometimes.
No, it should work in any conforming implementation.
It is not optional to do this type coercion if you multiply by a FLOAT
or especially if you use the function named FLOAT.