c3c icon indicating copy to clipboard operation
c3c copied to clipboard

sincos cannot be called

Open TheOnlySilverClaw opened this issue 1 year ago • 11 comments

I'm at a loss about how to call some macros. For example, sincos confusingly has two arguments, of which the second seems to be an array of two elements. I've tried a few combinations like this:

double[2] angles;
math::sincos(0.5, &angles);

But either the compiler doesn't accept my argument type or the contract is violated:

Error: @require "(@typekind(y) == ARRAY || @typekind(y) == VECTOR) &&& y.len == 2" violated.

But when logging out the typekind and len for angles, they seem to be just fine.

In this case, the macro should preferably accept a separate &sin and &cos pointer like the native version.

TheOnlySilverClaw avatar Oct 08 '24 10:10 TheOnlySilverClaw

This was a plain bug. I've done two changes:

  1. The old sincos interface was already broken, so I fixed it to use two separate pointers. I renamed that one sincos_ref
  2. I added a new sincos which would return sincos as a vector.

lerno avatar Oct 08 '24 18:10 lerno

Tried it with the fixed version and now it gives

undefined reference to `__sincos'

TheOnlySilverClaw avatar Oct 08 '24 20:10 TheOnlySilverClaw

What platform?

lerno avatar Oct 08 '24 20:10 lerno

Linux Mint x64

TheOnlySilverClaw avatar Oct 08 '24 20:10 TheOnlySilverClaw

That's odd. If you compile C with sincos, what name is it using in the linker? _sincos or __sincos or ___sincos?

lerno avatar Oct 08 '24 22:10 lerno

What would be the simplest way to find out? I haven't delved that deep into linker issues, yet.

TheOnlySilverClaw avatar Oct 09 '24 12:10 TheOnlySilverClaw

It looks like possibly on linux it isn't available.

lerno avatar Oct 09 '24 13:10 lerno

Apparently:

#include <math.h>

int main() {

    double sin, cos;
    sincos(0.5, &sin, &cos);

    return 0;
}

sincos.c:4:5: error: call to undeclared function 'sincos'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 4 | sincos(0.5, &sin, &cos);

This answer works: https://stackoverflow.com/a/61451065

#define _GNU_SOURCE

#include <math.h>
...

TheOnlySilverClaw avatar Oct 09 '24 13:10 TheOnlySilverClaw

It seems to be non-standard in any case. If you change the definition in math.c3 where it says @extern("__sincos") to @extern("sincos"), does it work then?

lerno avatar Oct 09 '24 14:10 lerno

Yes, that works. For sincos_ref, too.

TheOnlySilverClaw avatar Oct 09 '24 14:10 TheOnlySilverClaw

Try it now, pulling the latest.

lerno avatar Oct 09 '24 14:10 lerno

This works now although the error message when returning the wrong type is not very clear. Error: Arrays of different size may not be converted.

sandervdbosch avatar Nov 08 '24 14:11 sandervdbosch

@sandervdbosch can you show me your code example?

lerno avatar Nov 08 '24 14:11 lerno

import std::io;
import std::math;

fn void main(){
    float argF = 0.5;
    double argD = 0.5;

    float[2] anglesF = math::sincos(argF);
    double[2] anglesD = math::sincos(argF); // Will not work because input/output type mismatch
}

sandervdbosch avatar Nov 08 '24 15:11 sandervdbosch

@sandervdbosch This is because there is no conversion between float[2] and double[2]. The easiest would be to pass a double to sincos. This actually raised a more detailed problem about array conversions, and it's now possible to splat arrays into initializers. This allow us to write the last one as:

fn void main() 
{
  double[2] anglesD = { ...math::sincos(argF) };
}

This effectively splats the two floats into the initializer and the normal widening occurs.

There is another possibility, and that is to use vectors:

double[2] anglesD = (double[<2>])(float[<2>])math::sincos(argF);

Not very elegant due to the double casts though.

However if anglesD is a vector then we get the simpler:

double[<2>] anglesD = (float[<2>])math::sincos(argF);

lerno avatar Nov 16 '24 09:11 lerno

Oh, and I consequently also updated the error messages you're getting @sandervdbosch

lerno avatar Nov 16 '24 09:11 lerno

Looks much better now, thanks.

sandervdbosch avatar Nov 19 '24 07:11 sandervdbosch