Incorrect floating point values when using GHC API
Summary
On an M1 Mac, floating point literals are interpreted incorrectly by the GHC API when using compileExpr
. For instance (full code further below):
let chck = "5.5626902089526504e-303 :: Double"
v <- compileExpr chck
print (unsafeCoerce v :: Double)
prints
6.213763703753626e-303
On the Haskell discourse, other users with ARM processors have reported getting the correct behavior (not seeing this bug): https://discourse.haskell.org/t/different-results-from-ghc-api-and-ghci/10988/8
Steps to reproduce
On my (presumably any) M1 Mac, using GHC 9.6.6, 9.8.4, and 9.10.1, the below code:
module Main where
import GHC
import GHC.Paths
import Unsafe.Coerce
import Control.Monad.IO.Class
main :: IO ()
main = do
runGhc (Just libdir) (do
dyn_flags <- getSessionDynFlags
_ <- setSessionDynFlags dyn_flags
setContext [ IIDecl . simpleImportDecl . mkModuleName $ "Prelude"
, IIDecl . simpleImportDecl . mkModuleName $ "Unsafe.Coerce" ]
wrong)
wrong :: Ghc ()
wrong = do
let chck = "5.5626902089526504e-303 :: Double"
v <- compileExpr chck
liftIO $ do
putStr "Direct: "
print (5.5626902089526504e-303 :: Double)
putStr "API: "
print (unsafeCoerce v :: Double)
let chck2 = "5.56269020895265e-303 :: Double"
v2 <- compileExpr chck2
liftIO $ do
putStr "Direct: "
print (5.56269020895265e-303 :: Double)
putStr "API: "
print (unsafeCoerce v2 :: Double)
outputs:
Direct: 5.5626902089526504e-303
API: 6.213763703753626e-303
Direct: 5.56269020895265e-303
API: 5.2321149654365974e-303
Notice that something about going through compileExpr
turns 5.5626902089526504e-303
into 6.213763703753626e-303
, and 5.56269020895265e-303
into 5.2321149654365974e-303
.
C Compiler + building and running:
billhallahan@Bills-MacBook-Pro-2 APITest % cc -v
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
billhallahan@Bills-MacBook-Pro-2 APITest % cabal build
Resolving dependencies...
Build profile: -w ghc-9.10.1 -O1
In order, the following will be built (use -v for more details):
- APITest-0.1.0.0 (exe:APITest) (first run)
Configuring executable 'APITest' for APITest-0.1.0.0...
Warning: [unknown-file] The 'license-file' field refers to the file 'LICENSE'
which does not exist.
Preprocessing executable 'APITest' for APITest-0.1.0.0...
Building executable 'APITest' for APITest-0.1.0.0...
Glasgow Haskell Compiler, Version 9.10.1, stage 2 booted by GHC version 9.6.2
*** initializing unit database:
package flags [-package-id base-4.20.0.0-380b{unit base-4.20.0.0-380b True ([])},
-package-id ghc-9.10.1-64dd{unit ghc-9.10.1-64dd True ([])},
-package-id ghc-pths-0.1.0.12-5ab02b64{unit ghc-pths-0.1.0.12-5ab02b64 True ([])}]
loading package database /Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/package.conf.d
loading package database /Users/billhallahan/.cabal/store/ghc-9.10.1-64dd/package.db
loading package database /Users/billhallahan/Documents/APITest/dist-newstyle/packagedb/ghc-9.10.1
loading package database /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/package.conf.inplace
wired-in package ghc-prim mapped to ghc-prim-0.11.0-e020
wired-in package ghc-bignum mapped to ghc-bignum-1.3-09c4
wired-in package ghc-internal mapped to ghc-internal-9.1001.0-4685
wired-in package base mapped to base-4.20.0.0-380b
wired-in package rts mapped to rts-1.0.2
wired-in package template-haskell mapped to template-haskell-2.22.0.0-e0ca
wired-in package ghc-experimental mapped to ghc-experimental-0.1.0.0-2008
!!! initializing unit database: finished in 12.64 milliseconds, allocated 2.335 megabytes
*** Chasing dependencies:
Chasing modules from: APITest-0.1.0.0-inplace-APITest:app/Main.hs
!!! Chasing dependencies: finished in 1.16 milliseconds, allocated 2.182 megabytes
Ready for upsweep
[SingleModule(APITest-0.1.0.0-inplace-APITest:Main [])]
compile: input file app/Main.hs
*** Checking old interface for Main (use -ddump-hi-diffs for more details):
[1 of 1] Compiling Main ( app/Main.hs, /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.o )
*** Parser [Main]:
!!! Parser [Main]: finished in 0.37 milliseconds, allocated 1.086 megabytes
*** Renamer/typechecker [Main]:
!!! Renamer/typechecker [Main]: finished in 133.67 milliseconds, allocated 121.107 megabytes
*** Desugar [Main]:
Result size of Desugar (before optimization)
= {terms: 338, types: 284, coercions: 11, joins: 0/8}
*** Core Linted result of Desugar (before optimization):
Result size of Desugar (after optimization)
= {terms: 227, types: 182, coercions: 4, joins: 0/0}
*** Core Linted result of Desugar (after optimization):
!!! Desugar [Main]: finished in 1.10 milliseconds, allocated 2.450 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 491, types: 567, coercions: 584, joins: 0/16}
*** Core Linted result of Simplifier:
Result size of Simplifier iteration=2
= {terms: 415, types: 469, coercions: 255, joins: 0/6}
*** Core Linted result of Simplifier:
Result size of Simplifier iteration=3
= {terms: 397, types: 439, coercions: 215, joins: 0/2}
*** Core Linted result of Simplifier:
Result size of Simplifier iteration=4
= {terms: 393, types: 435, coercions: 215, joins: 0/0}
*** Core Linted result of Simplifier:
Result size of Simplifier
= {terms: 393, types: 435, coercions: 215, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 49.26 milliseconds, allocated 66.224 megabytes
*** Specialise [Main]:
Result size of Specialise
= {terms: 393, types: 435, coercions: 215, joins: 0/0}
*** Core Linted result of Specialise:
!!! Specialise [Main]: finished in 0.64 milliseconds, allocated 1.591 megabytes
*** Float out(FOS {Lam = Just 0,
Consts = True,
OverSatApps = False}) [Main]:
Result size of Float out(FOS {Lam = Just 0, Consts = True, OverSatApps = False})
= {terms: 547, types: 630, coercions: 215, joins: 0/0}
*** Core Linted result of Float out(FOS {Lam = Just 0, Consts = True, OverSatApps = False}):
!!! Float out(FOS {Lam = Just 0,
Consts = True,
OverSatApps = False}) [Main]: finished in 1.05 milliseconds, allocated 2.226 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 421, types: 465, coercions: 215, joins: 0/0}
*** Core Linted result of Simplifier:
Result size of Simplifier
= {terms: 421, types: 465, coercions: 215, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 2.10 milliseconds, allocated 5.122 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 465, types: 517, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
Result size of Simplifier
= {terms: 459, types: 508, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 5.03 milliseconds, allocated 9.488 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
Result size of Simplifier
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 2.90 milliseconds, allocated 5.515 megabytes
*** Float inwards [Main]:
Result size of Float inwards
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Float inwards:
!!! Float inwards [Main]: finished in 0.45 milliseconds, allocated 1.475 megabytes
*** Called arity analysis [Main]:
Result size of Called arity analysis
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Called arity analysis:
!!! Called arity analysis [Main]: finished in 0.50 milliseconds, allocated 1.485 megabytes
*** Simplifier [Main]:
Result size of Simplifier
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 0.86 milliseconds, allocated 2.472 megabytes
*** Demand analysis (including Boxity) [Main]:
Result size of Demand analysis (including Boxity)
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Demand analysis (including Boxity):
!!! Demand analysis (including Boxity) [Main]: finished in 164.39 milliseconds, allocated 180.421 megabytes
*** Constructed Product Result analysis [Main]:
Result size of Constructed Product Result analysis
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Constructed Product Result analysis:
!!! Constructed Product Result analysis [Main]: finished in 0.44 milliseconds, allocated 1.293 megabytes
*** Worker Wrapper binds [Main]:
Result size of Worker Wrapper binds
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Worker Wrapper binds:
!!! Worker Wrapper binds [Main]: finished in 0.40 milliseconds, allocated 1.255 megabytes
*** Simplifier [Main]:
Result size of Simplifier
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 0.96 milliseconds, allocated 2.491 megabytes
*** Exitification transformation [Main]:
Result size of Exitification transformation
= {terms: 467, types: 510, coercions: 252, joins: 0/0}
*** Core Linted result of Exitification transformation:
!!! Exitification transformation [Main]: finished in 0.40 milliseconds, allocated 1.210 megabytes
*** Float out(FOS {Lam = Just 0,
Consts = True,
OverSatApps = True}) [Main]:
Result size of Float out(FOS {Lam = Just 0, Consts = True, OverSatApps = True})
= {terms: 501, types: 531, coercions: 252, joins: 0/0}
*** Core Linted result of Float out(FOS {Lam = Just 0, Consts = True, OverSatApps = True}):
!!! Float out(FOS {Lam = Just 0,
Consts = True,
OverSatApps = True}) [Main]: finished in 0.68 milliseconds, allocated 2.065 megabytes
*** Common sub-expression [Main]:
Result size of Common sub-expression
= {terms: 493, types: 531, coercions: 249, joins: 0/0}
*** Core Linted result of Common sub-expression:
!!! Common sub-expression [Main]: finished in 3.27 milliseconds, allocated 1.759 megabytes
*** Float inwards [Main]:
Result size of Float inwards
= {terms: 493, types: 531, coercions: 249, joins: 0/0}
*** Core Linted result of Float inwards:
!!! Float inwards [Main]: finished in 0.47 milliseconds, allocated 1.526 megabytes
*** Simplifier [Main]:
Result size of Simplifier iteration=1
= {terms: 461, types: 509, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
Result size of Simplifier
= {terms: 461, types: 509, coercions: 252, joins: 0/0}
*** Core Linted result of Simplifier:
!!! Simplifier [Main]: finished in 1.77 milliseconds, allocated 4.952 megabytes
*** Demand analysis [Main]:
Result size of Demand analysis
= {terms: 461, types: 509, coercions: 252, joins: 0/0}
*** Core Linted result of Demand analysis:
!!! Demand analysis [Main]: finished in 1.41 milliseconds, allocated 2.957 megabytes
*** CoreTidy [Main]:
!!! CoreTidy [Main]: finished in 0.35 milliseconds, allocated 0.909 megabytes
Result size of Tidy Core
= {terms: 461, types: 509, coercions: 252, joins: 0/0}
*** Core Linted result of Tidy Core:
Created temporary directory: /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22891_0
*** LatePlugins [Main]:
!!! LatePlugins [Main]: finished in 0.00 milliseconds, allocated 0.013 megabytes
*** CorePrep [Main]:
Result size of CorePrep
= {terms: 511, types: 548, coercions: 252, joins: 0/11}
*** Core Linted result of CorePrep:
!!! CorePrep [Main]: finished in 0.65 milliseconds, allocated 1.757 megabytes
*** CoreToStg [Main]:
*** Stg2Stg:
!!! CoreToStg [Main]: finished in 1.56 milliseconds, allocated 0.880 megabytes
*** CodeGen [Main]:
!!! CodeGen [Main]: finished in 5.76 milliseconds, allocated 13.732 megabytes
*** WriteIface [/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.hi]:
writeBinIface: 136 Names
writeBinIface: 368 dict entries
!!! WriteIface [/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.hi]: finished in 0.74 milliseconds, allocated 1.437 megabytes
*** systool:as:
*** Assembler:
gcc '--target=arm64-apple-darwin' -Qunused-arguments -iquoteapp -I/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp -I/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp -I/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/autogen -I/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/global-autogen -I/Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp -fno-common -U__PIC__ -D__PIC__ -x assembler -c /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22891_0/ghc_1.s -o /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.o.tmp
!!! systool:as: finished in 1.04 milliseconds, allocated 0.187 megabytes
*** Deleting temp files:
Deleting: /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22891_0/ghc_1.s /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22891_0/ghc_2.c
Warning: deleting non-existent /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22891_0/ghc_2.c
*** Deleting temp subdirs:
Deleting:
Upsweep completely successful.
*** Deleting temp files:
Deleting:
*** Deleting temp subdirs:
Deleting:
*** Deleting temp dirs:
Deleting: /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22891_0
Glasgow Haskell Compiler, Version 9.10.1, stage 2 booted by GHC version 9.6.2
*** initializing unit database:
package flags [-package-id base-4.20.0.0-380b{unit base-4.20.0.0-380b True ([])},
-package-id ghc-9.10.1-64dd{unit ghc-9.10.1-64dd True ([])},
-package-id ghc-pths-0.1.0.12-5ab02b64{unit ghc-pths-0.1.0.12-5ab02b64 True ([])}]
loading package database /Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/package.conf.d
loading package database /Users/billhallahan/.cabal/store/ghc-9.10.1-64dd/package.db
loading package database /Users/billhallahan/Documents/APITest/dist-newstyle/packagedb/ghc-9.10.1
loading package database /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/package.conf.inplace
wired-in package ghc-prim mapped to ghc-prim-0.11.0-e020
wired-in package ghc-bignum mapped to ghc-bignum-1.3-09c4
wired-in package ghc-internal mapped to ghc-internal-9.1001.0-4685
wired-in package base mapped to base-4.20.0.0-380b
wired-in package rts mapped to rts-1.0.2
wired-in package template-haskell mapped to template-haskell-2.22.0.0-e0ca
wired-in package ghc-experimental mapped to ghc-experimental-0.1.0.0-2008
!!! initializing unit database: finished in 11.29 milliseconds, allocated 2.335 megabytes
*** Chasing dependencies:
Chasing modules from: APITest-0.1.0.0-inplace-APITest:app/Main.hs
!!! Chasing dependencies: finished in 0.95 milliseconds, allocated 2.163 megabytes
Ready for upsweep
[SingleModule(APITest-0.1.0.0-inplace-APITest:Main []),
SingleModule(LN: [APITest-0.1.0.0-inplace-APITest:Main])]
compile: input file app/Main.hs
*** Checking old interface for Main (use -ddump-hi-diffs for more details):
[1 of 2] Skipping Main ( app/Main.hs, /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.o )
*** Deleting temp files:
Deleting:
*** Deleting temp subdirs:
Deleting:
link: hmi ...
Main
link: linkables are ...
LinkableM (2024-12-13 17:50:24.92123009 UTC) Main
[DotO /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.o]
link: pkg deps are ...
base
ghc-9.10.1-64dd
ghc-pths-0.1.0.12-5ab02b64
[2 of 2] Linking /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest
Created temporary directory: /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0
*** systool:cc:
*** C Compiler:
gcc -c /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_1.c -o /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_2.o -fno-common -U__PIC__ -D__PIC__ -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ffi -I/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/rts-1.0.2/include -Qunused-arguments '--target=arm64-apple-darwin'
!!! systool:cc: finished in 1.26 milliseconds, allocated 0.223 megabytes
*** systool:linker:
*** Linker:
gcc '--target=arm64-apple-darwin' -Qunused-arguments '--target=arm64-apple-darwin' -Wl,-no_fixup_chains -o /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest -lm /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest-tmp/Main.o -L/Users/billhallahan/.cabal/store/ghc-9.10.1-64dd/ghc-pths-0.1.0.12-5ab02b64/lib -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-9.10.1-64dd -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/semaphore-compat-1.0.0-e731 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/process-1.6.19.0-27d7 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/hpc-0.7.0.1-4943 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghci-9.10.1-bb28 -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-heap-9.10.1-e0ad -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-boot-9.10.1-d6fc -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-platform-0.1.0.0-36c7 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/directory-1.3.8.3-2b7f -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/unix-2.8.5.1-6d6f -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/time-1.12.2-c82d -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/filepath-1.5.2.0-a392 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/os-string-2.0.2-5a04 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/exceptions-0.10.7-b950 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/stm-2.5.3.1-9361 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/mtl-2.3.1-657d -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/transformers-0.6.1.1-ad7e -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/binary-0.8.9.2-ab44 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/containers-0.7-622f -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/bytestring-0.12.1.0-5f32 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/template-haskell-2.22.0.0-e0ca -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/pretty-1.1.3.6-1a72 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-boot-th-9.10.1-5379 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/deepseq-1.5.0.0-c88a -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/array-0.5.7.0-0e4b -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/base-4.20.0.0-380b -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-internal-9.1001.0-4685 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-bignum-1.3-09c4 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/ghc-prim-0.11.0-e020 -L/Users/billhallahan/.ghcup/ghc/9.10.1/lib/ghc-9.10.1/lib/../lib/aarch64-osx-ghc-9.10.1/rts-1.0.2 /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_2.o -Wl,-u,_ghczminternal_GHCziInternalziTopHandler_runIO_closure -Wl,-u,_ghczminternal_GHCziInternalziTopHandler_runNonIO_closure -Wl,-u,_ghczmprim_GHCziTuple_Z0T_closure -Wl,-u,_ghczmprim_GHCziTypes_True_closure -Wl,-u,_ghczmprim_GHCziTypes_False_closure -Wl,-u,_ghczminternal_GHCziInternalziPack_unpackCString_closure -Wl,-u,_ghczminternal_GHCziInternalziWeakziFinalizze_runFinalizzerBatch_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_stackOverflow_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_heapOverflow_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_allocationLimitExceeded_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_blockedIndefinitelyOnMVar_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_blockedIndefinitelyOnSTM_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_cannotCompactFunction_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_cannotCompactPinned_closure -Wl,-u,_ghczminternal_GHCziInternalziIOziException_cannotCompactMutable_closure -Wl,-u,_ghczminternal_GHCziInternalziIOPort_doubleReadException_closure -Wl,-u,_ghczminternal_GHCziInternalziControlziExceptionziBase_nonTermination_closure -Wl,-u,_ghczminternal_GHCziInternalziControlziExceptionziBase_nestedAtomically_closure -Wl,-u,_ghczminternal_GHCziInternalziEventziThread_blockedOnBadFD_closure -Wl,-u,_ghczminternal_GHCziInternalziConcziSync_runSparks_closure -Wl,-u,_ghczminternal_GHCziInternalziConcziIO_ensureIOManagerIsRunning_closure -Wl,-u,_ghczminternal_GHCziInternalziConcziIO_interruptIOManager_closure -Wl,-u,_ghczminternal_GHCziInternalziConcziIO_ioManagerCapabilitiesChanged_closure -Wl,-u,_ghczminternal_GHCziInternalziConcziSignal_runHandlersPtr_closure -Wl,-u,_ghczminternal_GHCziInternalziTopHandler_flushStdHandles_closure -Wl,-u,_ghczminternal_GHCziInternalziTopHandler_runMainIO_closure -Wl,-u,_ghczmprim_GHCziTypes_Czh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Izh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Fzh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Dzh_con_info -Wl,-u,_ghczmprim_GHCziTypes_Wzh_con_info -Wl,-u,_ghczminternal_GHCziInternalziPtr_Ptr_con_info -Wl,-u,_ghczminternal_GHCziInternalziPtr_FunPtr_con_info -Wl,-u,_ghczminternal_GHCziInternalziInt_I8zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziInt_I16zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziInt_I32zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziInt_I64zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziWord_W8zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziWord_W16zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziWord_W32zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziWord_W64zh_con_info -Wl,-u,_ghczminternal_GHCziInternalziStable_StablePtr_con_info -Wl,-u,_hs_atomic_add8 -Wl,-u,_hs_atomic_add16 -Wl,-u,_hs_atomic_add32 -Wl,-u,_hs_atomic_add64 -Wl,-u,_hs_atomic_sub8 -Wl,-u,_hs_atomic_sub16 -Wl,-u,_hs_atomic_sub32 -Wl,-u,_hs_atomic_sub64 -Wl,-u,_hs_atomic_and8 -Wl,-u,_hs_atomic_and16 -Wl,-u,_hs_atomic_and32 -Wl,-u,_hs_atomic_and64 -Wl,-u,_hs_atomic_nand8 -Wl,-u,_hs_atomic_nand16 -Wl,-u,_hs_atomic_nand32 -Wl,-u,_hs_atomic_nand64 -Wl,-u,_hs_atomic_or8 -Wl,-u,_hs_atomic_or16 -Wl,-u,_hs_atomic_or32 -Wl,-u,_hs_atomic_or64 -Wl,-u,_hs_atomic_xor8 -Wl,-u,_hs_atomic_xor16 -Wl,-u,_hs_atomic_xor32 -Wl,-u,_hs_atomic_xor64 -Wl,-u,_hs_cmpxchg8 -Wl,-u,_hs_cmpxchg16 -Wl,-u,_hs_cmpxchg32 -Wl,-u,_hs_cmpxchg64 -Wl,-u,_hs_xchg8 -Wl,-u,_hs_xchg16 -Wl,-u,_hs_xchg32 -Wl,-u,_hs_xchg64 -Wl,-u,_hs_atomicread8 -Wl,-u,_hs_atomicread16 -Wl,-u,_hs_atomicread32 -Wl,-u,_hs_atomicread64 -Wl,-u,_hs_atomicwrite8 -Wl,-u,_hs_atomicwrite16 -Wl,-u,_hs_atomicwrite32 -Wl,-u,_hs_atomicwrite64 -Wl,-u,_ghczminternal_GHCziInternalziStackziCloneStack_StackSnapshot_closure -Wl,-search_paths_first -Wl,-U,___darwin_check_fd_set_overflow -Wl,-undefined,dynamic_lookup -Wl,-dead_strip -lHSghc-pths-0.1.0.12-5ab02b64 -lHSghc-9.10.1-64dd -lHSsemaphore-compat-1.0.0-e731 -lHSprocess-1.6.19.0-27d7 -lHShpc-0.7.0.1-4943 -lHSghci-9.10.1-bb28 -lHSghc-heap-9.10.1-e0ad -lHSghc-boot-9.10.1-d6fc -lHSghc-platform-0.1.0.0-36c7 -lHSdirectory-1.3.8.3-2b7f -lHSunix-2.8.5.1-6d6f -lHStime-1.12.2-c82d -lHSfilepath-1.5.2.0-a392 -lHSos-string-2.0.2-5a04 -lHSexceptions-0.10.7-b950 -lHSstm-2.5.3.1-9361 -lHSmtl-2.3.1-657d -lHStransformers-0.6.1.1-ad7e -lHSbinary-0.8.9.2-ab44 -lHScontainers-0.7-622f -lHSbytestring-0.12.1.0-5f32 -lHStemplate-haskell-2.22.0.0-e0ca -lHSpretty-1.1.3.6-1a72 -lHSghc-boot-th-9.10.1-5379 -lHSdeepseq-1.5.0.0-c88a -lHSarray-0.5.7.0-0e4b -lHSbase-4.20.0.0-380b -lHSghc-internal-9.1001.0-4685 -lHSghc-bignum-1.3-09c4 -lHSghc-prim-0.11.0-e020 -lHSrts-1.0.2 -liconv -lm -ldl -lffi -Wl,-dead_strip_dylibs -Wl,-headerpad,8000
!!! systool:linker: finished in 6.67 milliseconds, allocated 2.310 megabytes
*** otool:
otool -L /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest
*** otool:
otool -l /Users/billhallahan/Documents/APITest/dist-newstyle/build/aarch64-osx/ghc-9.10.1/APITest-0.1.0.0/x/APITest/build/APITest/APITest
link: done
Upsweep completely successful.
*** Deleting temp files:
Deleting: /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_1.c /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_3.rsp /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_4.rsp /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0/ghc_2.o
*** Deleting temp subdirs:
Deleting:
*** Deleting temp dirs:
Deleting: /var/folders/n8/29m6tzk94mzc6r_t6h42mc8m0000gn/T/ghc22948_0
billhallahan@Bills-MacBook-Pro-2 APITest % cabal run
Direct: 5.5626902089526504e-303
API: 6.213763703753626e-303
Direct: 5.56269020895265e-303
API: 5.2321149654365974e-303
Code, include Cabal file: APITest.zip
Expected behavior
I would expect the output:
Direct: 5.5626902089526504e-303
API: 5.5626902089526504e-303
Direct: 5.56269020895265e-303
API: 5.56269020895265e-303
Additional testing:
This appears to have something to do with the floats being written as literals. The below program, which uses hex encoding/encodeFloat, outputs correct results:
module Main where
import GHC
import GHC.Paths
import Unsafe.Coerce
import Control.Monad.IO.Class
main :: IO ()
main = do
runGhc (Just libdir) (do
dyn_flags <- getSessionDynFlags
_ <- setSessionDynFlags dyn_flags
setContext [ IIDecl . simpleImportDecl . mkModuleName $ "Prelude"
, IIDecl . simpleImportDecl . mkModuleName $ "Unsafe.Coerce" ]
correct)
correct :: Ghc ()
correct = do
let chck = "unsafeCoerce (0x012e848200000001 :: Integer) :: Double"
v <- compileExpr chck
liftIO $ do
putStr "Direct: "
print (unsafeCoerce (0x012e848200000001 :: Integer) :: Double)
putStr "API: "
print (unsafeCoerce v :: Double)
let chck2 = "encodeFloat 8589943181934593 (-1057) :: Double"
v2 <- compileExpr chck2
liftIO $ do
putStr "Direct: "
print (encodeFloat 8589943181934593 (-1057) :: Double)
putStr "API: "
print (unsafeCoerce v2 :: Double)
Results:
Direct: 5.5626902089526504e-303
API: 5.5626902089526504e-303
Direct: 5.5626902089526504e-303
API: 5.5626902089526504e-303
Environment
- GHC version used: 9.10.1 to get the above output (also tried 9.6.6 and 9.8.4, same incorrect printed results.)
Optional:
- Operating System: MacOS Ventura 13.0
- System Architecture: Apple M1 Max, ARM
billhallahan@Bills-MacBook-Pro-2 APITest % uname -a
Darwin Bills-MacBook-Pro-2.local 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:15:09 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T6000 arm64
billhallahan@Bills-MacBook-Pro-2 APITest % cat /etc/motd
cat: /etc/motd: No such file or directory