Generic math macros on Linux


(Timothy Chagnon) #1

Hi folks,

I ran into this issue (or at least surprise) trying to compile some code
that uses math.h functions on both OSX and Linux. Here's an example:

#if os(Linux)
  import Glibc
#else
  import Darwin
#endif

let x = Float(1.2345)
let y = sin(x)
print(y)

Compiles and runs fine on OSX, but fails with the following error on Linux:
*MathTest.swift:8:13: error: cannot convert value of type 'Float' to
expected argument type 'Double'*

I think I tracked this down to the fact that Darwin includes the
additional tgmath
style macros
<https://github.com/apple/swift/blob/master/stdlib/public/SDK/Darwin/tgmath.swift.gyb#L44>
that provide the overloaded sin(x: Float) -> Float swift function. Glibc
only includes plain math.h functions:
double sin(double x);
float sinf(float x);

I can, of course, just use the explicit *f math.h functions on both
platforms. But I'm curious if this is a bug I should file or if this is
intentional to not have overloaded math functions in Swift. I couldn't
find much mention of it in the bug tracker.

Tim


(Dmitri Gribenko) #2

Yes, Glibc module is lacking an overlay for tgmath. Please file a bug.

Dmitri

···

On Wed, Jan 27, 2016 at 11:23 PM, Timothy Chagnon via swift-users <swift-users@swift.org> wrote:

Hi folks,

I ran into this issue (or at least surprise) trying to compile some code
that uses math.h functions on both OSX and Linux. Here's an example:

#if os(Linux)
  import Glibc
#else
  import Darwin
#endif

let x = Float(1.2345)
let y = sin(x)
print(y)

Compiles and runs fine on OSX, but fails with the following error on Linux:
MathTest.swift:8:13: error: cannot convert value of type 'Float' to expected
argument type 'Double'

I think I tracked this down to the fact that Darwin includes the additional
tgmath style macros that provide the overloaded sin(x: Float) -> Float swift
function. Glibc only includes plain math.h functions:
double sin(double x);
float sinf(float x);

I can, of course, just use the explicit *f math.h functions on both
platforms. But I'm curious if this is a bug I should file or if this is
intentional to not have overloaded math functions in Swift. I couldn't find
much mention of it in the bug tracker.

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/