Author Topic: Bad conversion of int to double  (Read 3143 times)

Offline rudolf128

  • Multiple posting newcomer
  • *
  • Posts: 29
Bad conversion of int to double
« on: May 01, 2019, 09:55:58 pm »
I have a rather large program of a Software Synthesizer. While modifying it, it gave me an error. I traced the error to a small routine, and there I found a failing conversion of int to double. I tried to construct a very small program that did the same thing, but the routine worked OK then. The original routine is as follows:

Code
#define REAL2 double

void Fadeinmag(void)
// "Magic" fade-in
{
  REAL2 a, t;
  a = (Oldtime - Par.sr) / (REAL2)(Par.ed - Par.sr);
  t = a * a;
  Pars[Par.ix].fade = t / (1 + (t - a) * 2);
}

Par.ed, Par.sr and Oldtime are all int global variables. My PC is 64 bits, and I use Windows 10 and Mingw compiler and last Code::Blocks version, but I am not using any nightly build.
While debugging it with the standard debugger, I found that after being assigned, variable 'a' had a NAN value, which should instead be 0, because Par.ed = 352597, Par.sr = 350342, and Oldtime = 350342 at the time.
Any help will be appreciated.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7592
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org