<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:1741367202;
mso-list-type:hybrid;
mso-list-template-ids:1343753416 -1 134807555 134807557 134807553 134807555 134807557 134807553 134807555 134807557;}
@list l0:level1
{mso-level-number-format:bullet;
mso-level-text:-;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:54.0pt;
text-indent:-18.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-font-family:"Times New Roman";}
@list l0:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:90.0pt;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l0:level3
{mso-level-number-format:bullet;
mso-level-text:\F0A7;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:126.0pt;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l0:level4
{mso-level-number-format:bullet;
mso-level-text:\F0B7;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:162.0pt;
text-indent:-18.0pt;
font-family:Symbol;}
@list l0:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:198.0pt;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l0:level6
{mso-level-number-format:bullet;
mso-level-text:\F0A7;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:234.0pt;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l0:level7
{mso-level-number-format:bullet;
mso-level-text:\F0B7;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:270.0pt;
text-indent:-18.0pt;
font-family:Symbol;}
@list l0:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:306.0pt;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l0:level9
{mso-level-number-format:bullet;
mso-level-text:\F0A7;
mso-level-tab-stop:none;
mso-level-number-position:left;
margin-left:342.0pt;
text-indent:-18.0pt;
font-family:Wingdings;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
--></style>
</head>
<body lang="EN-GB" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hello all – I’m trying to debug a problem with asinh in Windows.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">On Linux (Ubuntu 18.04.5 LTS) running GHC 8.10.7, I get (I think correctly):</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">Prelude> asinh 1.7976931348623157e308<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:#00B050">710.4758600739439<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But on Windows I get:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">GHCi, version 8.10.7: https://www.haskell.org/ghc/ :? for help<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">Prelude> asinh 1.7976931348623157e308<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:red">NaN<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">My understanding is that, since <a href="https://gitlab.haskell.org/ghc/ghc/-/commit/c6f4eb4f8bc5e00024c74198ab9126bf1750db40">
commit c6f4eb4f8</a> (released in GHC 8.8.1, I think), asinh is defined as a primop that just calls the c asinh function, so I tried the following code:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In Main.hs:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">{-# LANGUAGE ForeignFunctionInterface #-}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">{-# LANGUAGE CApiFFI #-}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">module Main (main) where<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">import Foreign<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">import Foreign.C.Types<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">foreign import ccall unsafe "math.h asinh" c_asinh :: Double -> Double<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">foreign import ccall unsafe "CAsinh.c testFn" c_testFn :: IO ()<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">main :: IO ()<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">main = do<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> putStrLn $ "asinh 1.7976931348623157e308 = " ++ show ( asinh 1.7976931348623157e308)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> putStrLn $ "c_asinh 1.7976931348623157e308 = " ++ show (c_asinh 1.7976931348623157e308)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> putStrLn "Calling c_testFn..."<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> c_testFn<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In CAsinh.c:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">#include <stdio.h><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">#include <math.h><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">void testFn ()<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> printf ("in testFn\n");<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> printf ("asinh(1.7976931348623157e308) = %f\n", asinh(1.7976931348623157e308));<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> double x = 1.7976931348623157e308;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New""> printf ("asinh(x) = %f\n", asinh(x));<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">}<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In Linux, this all works fine:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:#00B050">asinh 1.7976931348623157e308 = 710.4758600739439<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:#00B050">c_asinh 1.7976931348623157e308 = 710.4758600739439<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">Calling c_testFn...<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">in testFn<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:#00B050">asinh(1.7976931348623157e308) = 710.475860<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:#00B050">asinh(x) = 710.475860<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But on Windows I get weird results:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:red">asinh 1.7976931348623157e308 = NaN<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:red">c_asinh 1.7976931348623157e308 = NaN<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">Calling c_testFn...<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New"">in testFn<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:#00B050">asinh(1.7976931348623157e308) = 710.475860<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family:"Courier New";color:red">asinh(x) = -1.#IND00<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The primop call from Haskell to asinh and the FFI call to c_asinh give the same (incorrect) result, as I expected. But the first call to asinh from the c testFn gives the correct result. So why is that different to the FFI call from Haskell?</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">And why doesn’t the second call from testFn return the right result?</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I also tested on 9.0.1 on Windows, with a slightly different result for the final asinh:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b><span style="font-family:"Courier New"">...<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span style="font-family:"Courier New"">asinh(x) = nan<o:p></o:p></span></b></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’ve investigated the things I can think of (different foreign declarations, size of double being different, checking the code in
<a href="http://cpp.sh/">cpp.sh</a>), but can’t find an explanation. Am I doing something silly? Is this a (known) bug?</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Note that on Windows, values up to asinh 1.3407807929942596e154 work correctly.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">(As a little background: I was trying to implement the <a href="https://people.freebsd.org/~das/kahan86branch.pdf">
Kahan</a> functions to give the correct branch cuts for complex trig functions, something I think the current Haskell implementation doesn’t do correctly. These depend on asinh, etc, on RealFloat numbers).</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks very much for any help,</p>
<p class="MsoNormal">David.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoListParagraph" style="margin-left:18.0pt;mso-list:l0 level1 lfo1"><o:p> </o:p></li></ul>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>