-2

i am trying to write a function that repeatedly adds 0.001 to 't' and then plugs it into 'y' until 't' reaches 0.3 however the numbers come out wrong, but i've noticed that if i change float to int and change the numbers to integer, the fuction works.. what should i change so the function works properly

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

void main(void)
{
    float t,y,dt;

    dt = 0.001;
    y = 1;
    t = 0;

    while (t <= 0.3)
    {
        y = y + dt*(sin(y)+(t)*(t)*(t));
        t = t + dt;
    }

    printf("y is %d when t is 0.3\n" , y);
    return 0;
}
Aemyl
  • 1,501
  • 1
  • 19
  • 34

1 Answers1

1

i've noticed that if i change float to int and change the numbers to integer, the fuction works.. what should i change so the function works properly

as said in a remark the problem is the way you (try to) print the value in

printf("y is %d when t is 0.3\n" , y);

%d suppose the corresponding argument is an int and prints it as an int, but y is a float. Note that there is no conversion from float to int in that case because the arguments are managed through a varargs

just do

 printf("y is %f when t is 0.3\n" , y);

Also change

void main(void)

to

int main()

After the changes, compilation and execution :

/tmp % gcc -pedantic -Wall -Wextra f.c -lm
/tmp % ./a.out
y is 1.273792 when t is 0.3

Note that all the calculations are done in double, so better to replace float to double to type your vars


(edit) Compiling your initial code with gcc and the option -Wall signals your problems :

/tmp % gcc -Wall f.c -lm
f.c:4: warning: return type of 'main' is not 'int'
f.c: In function 'main':
f.c:18: warning: format '%d' expects type 'int', but argument 2 has type 'double'
f.c:19: warning: 'return' with a value, in function returning void

To use both -Wall and -Wextra is the better option

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
bruno
  • 32,421
  • 7
  • 25
  • 37
  • Why are you using `-Wextra` but not `-Wall`? – klutt Mar 21 '19 at 08:04
  • @Broman to have more than with Wall. Wextra enables some extra warning flags that are not enabled by Wall , see https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html – bruno Mar 21 '19 at 08:06
  • Where did Werror come from? That's something completely different. My question is why you're NOT using Wall. That's highly unusual when you're using Wextra. – klutt Mar 21 '19 at 08:07
  • @Broman sorry I written Werror ... by error, I edited my remark to replace it by Wextra. _Wextra_ includes _Wall_, and does more. The more the compiler signals errors/warning the better it is – bruno Mar 21 '19 at 08:09
  • Nope, Wextra does NOT include Wall. They are separate. – klutt Mar 21 '19 at 08:11
  • Try with this program: `int main() { int x; }` – klutt Mar 21 '19 at 08:12
  • @broman please read https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html , _-Wextra : This enables some extra warning flags that are not enabled by -Wall_ – bruno Mar 21 '19 at 08:12
  • That will emit a warning with Wall, but not Wextra – klutt Mar 21 '19 at 08:12
  • 1
    Yes, nowhere does it say that it also enables everything that Wall enables. Try it out for yourself. – klutt Mar 21 '19 at 08:12
  • @Broman ahhh, you mean why I don't use Wextra *and* Wall. Effectively you are right, I supposed Wextra includes Wall but it seems not, so need to use both of htem. Thank you for the advice and sorry for my misunderstanding / quiproquo ^^ – bruno Mar 21 '19 at 08:14
  • Yes that's what I mean. I would really recommend using both. – klutt Mar 21 '19 at 08:17
  • There are actually some warnings that need both to be activated. Wunused-parameter is an example. Try this code: `void foo(int x) {} int main() {}` It will not give a warning if you're only using one of Wall and Wextra. – klutt Mar 21 '19 at 08:21
  • @Broman yes, but for me that change nothing because I consider a warning as an error by myself ;-) – bruno Mar 21 '19 at 08:31