Hi Taylor,
I do agree but it's only *mostly* absurd I would argue. :)
If I know I have a flonum within a known range, and I always want it as a fixnum, then this does the job, and I imagine skips a bunch of checks that would be irrelevant in this case (pls correct me if wrong though!)
If you use (##c-code ... ) inline then the C callout is gone... and on GCC / Intel the conversion will compile to a cvttsd2si instruction.
I am just putting a pragmatic / performance view here, it's definitely not elegant.
Obviously if inexact->exact has higher performance, eg because it's an optimizable intrinsic, then this method is categorically rubbish. :) I haven't checked yet though.
Cheers, Darren
Taylor R Campbell wrote:
Date: Thu, 30 Oct 2008 08:37:32 +1000 From: Darren Baker darren@wildfire.com.au
This is no doubt Very Bad Practice but it's useful :)
(define flonum->fixnum (c-lambda (double) int "___result = (int)___arg1;"))
That's absurd! Aside from the obvious absurdity of calling out to C for this, it leads to nasal demons if the range of the argument's integral part exceeds the range of C ints or Gambit fixnums. There is a perfectly good standard procedure to obtain the nearest exact number to an inexact one, which one may have obtained by rounding another inexact number. This procedure is called INEXACT->EXACT.