Skip to content

Missing RULEs for truncate

I found that the rounding functions from RealFrac class are considerably slower than the low level functions from GHC.Float. This is really a problem for me when doing signal processing, since for writing to a common audio file format or listening to a signal data has to be converted from Double to Int16.

$ ghci +RTS -M256m -c30 -RTS

   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.4.1, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base-1.0 ... linking ... done.
Prelude> :set +s
Prelude> sum $ map round [0.1,0.32..100000] :: Int
1252489463
(6.50 secs, 241894764 bytes)
Prelude> sum $ map floor [0.1,0.32..100000] :: Int
1252262188
(6.07 secs, 240099200 bytes)
Prelude> sum $ map ceiling [0.1,0.32..100000] :: Int
1252716734
(6.13 secs, 243795404 bytes)
Prelude> sum $ map truncate [0.1,0.32..100000] :: Int
1252262188
(6.76 secs, 234572324 bytes)
Prelude> sum $ map GHC.Float.double2Int [0.1,0.32..100000] :: Int
1252262188
(1.38 secs, 66137016 bytes)

As far as I can judge, double2Int does the same like truncate. Instead of using the methods from RealFrac I could simply use double2Int but I consider this a work-around.

In GHC-6.6.1 these examples end with a stack overflow, but if I shorten the list, the time relations remain the same.

Trac metadata
Trac field Value
Version 6.4.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC ghc-bug@henning-thielemann.de
Operating system Linux
Architecture Unknown
Edited by Simon Marlow
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information