-2

I am experimenting with some trigonometric functions, the code compile good, i want see a square moving on the 360 degrees, but when i run the code i see the square only move in the x-axis, everything work fine except a line on my function.

void mvdr(int* x, int* y, float d, float sp)
{
    if(d != 0)
        d = (d / 360.0) / 6.283185307179586;

    *x += cos(d)*sp;
    *y += sin(d)*sp; // here's the error
}

I saw in gdb: y doesn't change.

(gdb) n
11       *y += sin(d)*sp; // here's the error
(gdb) p d
$4 = 0.000442097051
(gdb) p sin(d)*sp
$5 = 6.44245094e+09
(gdb) p *y
$6 = 256
(gdb) n
12       }
(gdb) p *y
$7 = 256

I can't understand what is happening or if i did something wrong, i hope you can help me.

ZarWare
  • 25
  • 2
  • "I saw in gdb: y doesn't change." You are not changing `y` but what it points to with `*y`. – Weather Vane Jul 04 '17 at 23:41
  • 1
    @WeatherVane that's exactly what he's looking at in GDB – iehrlich Jul 04 '17 at 23:41
  • Not what was asked. `y` does not change. – Weather Vane Jul 04 '17 at 23:43
  • 2
    `*y` is an `int` value. Perhaps the `float` change is too small to register. Show some example values. And why are you still using `float` in the 22nd century? – Weather Vane Jul 04 '17 at 23:51
  • @WeatherVane: my hypothesis: `scanf("%f", &float_var)` is easy to get right; remembering to use `scanf("%lf", &double_var)` is harder. Once upon a time, it might have been for novelty's sake — pre-standard C did all floating point arithmetic in `double`, of course. – Jonathan Leffler Jul 05 '17 at 00:01
  • 1
    Coincidence? "6.44245094e+09" is a valid way to print `0x180000000`. But that would still change a 32-bit `int`. – aschepler Jul 05 '17 at 00:04

1 Answers1

2

To convert degrees to radians you should:

d = (d / 360.0) * 6.283185307179586;

Your conversion function results in very small values which for sin() may not change the integer value of y and for cos() result in larger values for x

anneb
  • 5,510
  • 2
  • 26
  • 26