-4

I am learned about C# but I want to learn C and then C++ for OOP. now I am working with library math.h and function of pow(x,y) in C.
I am writing a program with this code:
enter image description here
I get something very strange. It's result of that:
enter image description here

My problem is about pow. what does it do?
Is it possible to explain for me why this things showed? I can't understand.
Thanks for help and I'm sorry for my English too.

  • 2
    Please don't put printscreens of errors, put the actual message you have in the question, as text. Also, learning C++ for OOP when you're already learning C#, really? – AntonH Oct 31 '17 at 19:37
  • 4
    `1/4 = 0` since since `1` and `4` are both integer literals. When you divide them, you get an integer as the result with the remainder lopped off. – Christian Gibbons Oct 31 '17 at 19:41
  • I update it. "learning C++ for OOP when you're already learning C#" because C++ need low memory and I have to use it in some programs. – Amin Borjian Oct 31 '17 at 19:45
  • In addition the issue that integer division results in integers, test cases #5 and #6 aren't actually testing what you think (you're calling `printf()` with the result of some other `pow()` expression, *not* with `number`), and the last test case (`pow(3200000, 0.20)`) did produce the correct result. – jamesdlin Oct 31 '17 at 19:47
  • And adding on to what AntonH said, please don't post screenshots of the code you'r asking about either. Copy and paste. – jamesdlin Oct 31 '17 at 19:49
  • Oh. Ok I'm understanding. – Amin Borjian Oct 31 '17 at 19:54
  • I delete it because it's my mistake. Thanks. – Amin Borjian Oct 31 '17 at 19:55
  • Post code here, as text, to add clarity to the question. – chux - Reinstate Monica Oct 31 '17 at 19:56
  • It works properly now. It's my mistake. sorry. just overlock my fault. – Amin Borjian Oct 31 '17 at 20:07

2 Answers2

1

The main issue with your code is that you must understand how operations are handled for the several native types C can handle.

The divide operation (/) will return an integer if both operators are integers.

That is:

1 / 4 == 0

and not 0.25 as it seems you're expecting.

Therefore, in line:

number = pow(160000, 1 / 4);

what you're actually executing is:

number = pow(160000, 0);

and we know that any base raised to the 0 power is equals to 1

1

The compiler treats '1/4' as integer division.

What this means is that it first does the division which evaluates to 0.25 then casts it to an integer (discard the decimal) leaving you with 0. And any number raised to the power of 0 is 1.

Adding the decimal points after the 1 and 4 lets the compiler know that you want the result of your division to be a double so it keeps the result as 0.25.

Here's some example code:

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

int main()
{
    double number = 1/4;
    printf("Integer Division: %f\n", number); //prints 0

    number = 1.0/4.0;
    printf("Double Division: %f\n", number); //prints 0.25

    return 0;
}
Eltee
  • 87
  • 3