Home > Mobile >  Is it possible for a c function to accept both double and long double arguments?
Is it possible for a c function to accept both double and long double arguments?

Time:04-08

I have a function, mag, in a file mag.c that calculates the magnitude of an array.

#include <math.h>
#include "mag.h"

long double mag(long double arr[], int len){
    long double magnitude=0;
    for(int i=0;i<len;i  )
        magnitude =pow(arr[i],2);
    magnitude=pow(magnitude,0.5);
    return magnitude;
}

I would like it to accept both double and long double arrays. I've read elsewhere (for instance here) that if an argument doesn't match the function declaration, it will be implicitly converted to the correct type. I wrote a function test.c to test this.

#include <math.h>
#include <stdio.h>
#include "mag.h"

int main(){
        double arr1[3]={0.0,1.1,2.2};
        printf("%Lf",mag(arr1,3));
        return 0;
}

However, this produced an error

test.c: In function ‘main’:
test.c:7:19: warning: passing argument 1 of ‘mag’ from incompatible pointer type [-Wincompatible-pointer-types]
  printf("%Lf",mag(arr1,3));
                   ^~~~
In file included from test.c:3:
mag.h:4:29: note: expected ‘long double *’ but argument is of type ‘double *’
 long double mag(long double arr[], int len);

Declaring the array as a long double allows the function to work properly. I also tried changing the argument type in the header file, but that returned -nan. Is there any easy way to make the mag function accept both double and long double arguments, or would it be simpler to make 2 separate functions? (If I need to make separate functions for double and long double arguments, I would need to do this for a lot of files.)

CodePudding user response:

... it will be implicitly converted to the correct type. I wrote a function test.c to test this.

This works for some arguments like double converted to long double, but not double * converted to long double *.

C has _Generic just for this sort of programming. Use mag(arr, len) _Generic((arr) ... to steer selection of code.

I recommend to also use long double functions with the long double objects.

long double mag_long_double(const long double arr[], int len) {
  long double magnitude = 0;
  for (int i = 0; i < len; i  )
    magnitude  = powl(arr[i], 2);
  magnitude = sqrtl(magnitude);
  return magnitude;
}

double mag_double(const double arr[], int len) {
  double magnitude = 0;
  for (int i = 0; i < len; i  )
    magnitude  = pow(arr[i], 2);
  magnitude = sqrt(magnitude);
  return magnitude;
}

#define mag(arr, len) _Generic((arr), \
  long double *: mag_long_double, \
  double *: mag_double \
  )((arr), (len))

int main(void) {
  double arr1[3] = {0.0, 1.1, 2.2};
  printf("%f\n",mag(arr1,3));
  long double arr2[3] = {0.0, 3.3L, 4.4L};
  printf("%Lf\n",mag(arr2,3));
  return 0;
}

Output

2.459675
5.500000

CodePudding user response:

You are correct that a function argument will be implicitly converted to the expected type, if possible. However, this implicit conversion will not go so far as to convert the object or the array that the function argument is pointing to. It will only convert the function argument itself (i.e. the pointer).

Therefore, in C, you will have to create two separate functions, or do something ugly, like creating a single function which takes a void * as an argument, which will be interpreted either as a long double * or a double * depending on the value of an additional argument:

#include <math.h>
#include <stdbool.h>

long double mag( void *arr, int len, bool is_long )
{
    long double magnitude=0;

    if ( is_long )
    {
        //interpret array as "long double"
        long double *arr_longdouble = arr;

        for( int i=0; i<len; i   )
            magnitude  = powl( arr_longdouble[i], 2 );
    }
    else
    {
        //interpret array as "double"
        double *arr_double = arr;

        for( int i=0; i<len; i   )
            magnitude  = powl( arr_double[i], 2 );
    }

    magnitude = powl( magnitude, 0.5 );
    return magnitude;
}

However, it is worth nothing that C (but not C) has the ideal solution for what you want to accomplish. In that language, you can make a single function template:

template <typename T>
T mag( T arr[], int len )
{
    T magnitude = 0;
    for( int i=0; i<len; i   )
        magnitude  = pow( arr[i], 2 );
    magnitude = pow( magnitude, 0.5 );
    return magnitude;
}

In this function, the data type T can be either a double or a long double (or any other data type, as long as the code compiles). The compiler will automatically create a double and/or a long double version of the function for you, as required.

CodePudding user response:

You need to implement generic operators.

If you pass the arguments as generic vectors, you can use

long double mag(void* arr, int len)

Inside mag you should know the type of the elements you pass in arr. For this, you can tag the pointers in array. If you convert each void * pointer to some other pointer, and if you consider that pointer as an integer (via the type intptr_t), that integer is all the time a multiple of 8, which means, the first 3 lowest bits are not used. You can use these 3 bits to keep the type.

  • Related