2

In one of my assignments, I am supposed to get values of x^2, x^4 and cube root of x in which x is from 0 - 100. So far, I have this. (Testing with 5 numbers)

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

int powers(int n)
{
    return (n < 0) || (powers(n-1) && printf("%d\t%d\t%d\t\t%d\n", n, n*n, n*n*n*n, cbrt(n)));
}

int main(void)
{
    printf("number\tx^2\tx^4\t\tx^(1/3)\n");
    powers(5);

    return 0;
}

MY OUTPUT

number    x^2    x^4        x^(1/3)
0         0      0          0
1         1      1          0
2         4      16         -108170613
3         9      81         1225932534
4         16     256        -1522700739
5         25     625        -1124154156

So, my square and quatric are working as simple as it is but I cant get to work with cube root. When I do cube root separately it works. printf("Cube root of 125 is %f\n, cbrt(125)); yields Cube root of 125 is 5.0000.

I need help to why it does not work in my function. New to C programming so please be kind. (Compiler: Borland C++ and IDE: C-Free 5.0)

Ofek S.
  • 51
  • 1
  • 12
Imtiaz Raqib
  • 315
  • 2
  • 20
  • 4
    `cbrt` returns a `double`, so you should print the result with one of the floating-point formats: `%f`, `%g` or `%e`. Activate warnings to learn about such mismatches of printing formats and argument types. – M Oehm Apr 02 '16 at 17:43
  • @ColonelThirtyTwo What header should I use? – Imtiaz Raqib Apr 02 '16 at 17:44
  • @MOehm Ohmygod. IT WORKED. – Imtiaz Raqib Apr 02 '16 at 17:45
  • 2
    you can use `pow(n, 1/3)`. Where `n` is a number. – Md Mahfuzur Rahman Apr 02 '16 at 17:45
  • Just as a comment... you should create functions for the calculations and don't create such long lines. It will make your code harder to read. – Mark Apr 02 '16 at 17:48
  • The C header for the math library is ``. `` is the same header, but for C++. It puts the functions into the `std`namespace. C doesn't have user-defined namespaces. – M Oehm Apr 02 '16 at 17:49
  • @MdMahfuzurRahman, `pow(n, 1/3)` returns 1.0 (try it). `1/3` is integer division, so you've just written `pow(n, 0)` in an obfuscated manner. – Toby Speight Jan 23 '18 at 11:19

1 Answers1

2

The problem is that cbrt accepts and returns a float or double value, which means that cbrt(n) will automatically convert n to a float/double before passing it to the function. The function will return a float/double but you are not storing it anywhere to force a conversion back to an int, you are directly passing it to printf specifying %d so the value is interpreted as an int even though it is actually a float/double.

A simple cast would be enough: (int)cbrt(n), or you could use %f or %g specifier and print it as its real type.

Also storing in a temporary variable would lead to the same conversion behavior:

int v = cbrt(n);
printf("%d\n", v);
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Oh alright, will just using `printf("%f\n", cbrt(n))` do the trick as `%f` takes a float? – Imtiaz Raqib Apr 02 '16 at 17:50
  • 1
    @ImtiazRaqib: It's a bit more complicated than that: `printf` is a variadic function, and some arguments to such functions are "promoted". `char` and `short` are promoted to `int` and `float` is promoted to `double`. The upshot of this is that you can use `%f` and `%g` for both `float` and `double`. Activate warnings and let the compiler tell you about wrong formats. – M Oehm Apr 02 '16 at 18:02
  • @MOehm That makes a lot of sense. In my compiler they kept mentioning `promoted int to double` and I did not understand what it meant. Thank you. – Imtiaz Raqib Apr 02 '16 at 18:04
  • `cbrt` has type `double` as input and output, not float. `cbrtf` works on floats. – marcolz Jan 23 '18 at 11:51