File: num009.hs

package info (click to toggle)
ghc 9.10.3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 169,076 kB
  • sloc: haskell: 713,554; ansic: 84,184; cpp: 30,255; javascript: 9,003; sh: 7,870; fortran: 3,527; python: 3,228; asm: 2,523; makefile: 2,324; yacc: 1,570; lisp: 532; xml: 196; perl: 111; csh: 2
file content (44 lines) | stat: -rw-r--r-- 1,556 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- trac #2059
--


module Main(main) where

import Control.Monad
import Foreign.C

main = do let d = [0, pi, pi/2, pi/3, 1e10, 1e20] :: [Double]
              f = [0, pi, pi/2, pi/3, 1e10, 1e20] :: [Float]
          mapM_ (test "sind" sind sin) d
          mapM_ (test "sinf" sinf sin) f
          mapM_ (test "cosd" cosd cos) d
          mapM_ (test "cosf" cosf cos) f
          mapM_ (test "tand" tand tan) d
          mapM_ (test "tanf" tanf tan) f
          -- added to test #13629
          mapM_ (test "sqrtd" sqrtd sqrt) f
          mapM_ (test "sqrtf" sqrtf sqrt) f
          putStrLn "Done"

test :: (RealFloat a, Floating a, RealFloat b, Floating b, Show b)
     => String -> (a -> a) -> (b -> b) -> b -> IO ()
test s f g x = do let y = realToFrac (f (realToFrac x))
                      z = g x
                  unless (y == z) $ do
                      putStrLn ("uh oh! " ++ s ++ ' ':show x)
                      print y
                      print z
                      print $ decodeFloat y
                      print $ decodeFloat z

foreign import ccall "math.h sin"  sind :: CDouble -> CDouble
foreign import ccall "math.h sinf" sinf :: CFloat  -> CFloat

foreign import ccall "math.h cos"  cosd :: CDouble -> CDouble
foreign import ccall "math.h cosf" cosf :: CFloat  -> CFloat

foreign import ccall "math.h tan"  tand :: CDouble -> CDouble
foreign import ccall "math.h tanf" tanf :: CFloat  -> CFloat

foreign import ccall "math.h sqrt"  sqrtd :: CDouble -> CDouble
foreign import ccall "math.h sqrtf" sqrtf :: CFloat  -> CFloat