Question

I want a function number->second-pair that accepts a number and returns a pair of integer representing its integer part & fractional part multipled with 1000000.

i.e.:

(number->second-pair 1)
; returns (1 . 0)
; 1 sec -> (1 sec + 0 usec)
(number->second-pair 5.1234)
; returns (5 . 123400)
; 5.1234 sec -> (5 sec + 123400 usec)

It might be easy for you to find a solution, but I've searched many docs and sadly can't find the way to convert numbers to integers. Can someone help me out?

BTW:

Actually I want a preciser alarm(this one) by making use of setitimer, so I want exact integers passed as arguments.

Was it helpful?

Solution

I may be a bit rusty with scheme but I think something like this would work for you,

(define (number->second-pair n)
    (cons (inexact->exact (floor n))
          (inexact->exact (floor (* 1000000 (- n (floor n)))))))

(number->second-pair 5.1234) returns (5 . 123400)

(number->second-pair 1) returns (1 . 0)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top