2013-04-04 23 views
0

我想要一個功能number->second-pair接受一些並返回一對整數,代表其整數部分&小數部分與1000000 multipled如何在計劃中將拆分數字分解爲整數和小數部分?

即:

(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) 

這可能是您輕鬆找到解決方案,但我搜索了很多文檔,可惜找不到將數字轉換爲整數的方法。有人可以幫我嗎?

BTW:

其實我想通過利用setitimer一個更加精密alarmthis one),所以我想作爲參數傳遞準確的整數。

回答

2

我可能有點生疏的計劃,但我認爲這樣的事情會爲你工作,

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

(number->second-pair 5.1234)回報(5 . 123400)

(number->second-pair 1)回報(1 . 0)

+0

正是我想要的,謝謝: ) – Javran 2013-04-04 12:57:35

相關問題