This math is driving me crazy!

Why do the two below give different results? I need the second result to be the same as the first.

1
2
3
4
5
6
  float mySampleRate = 44100.0f;

  int phase = 4294967296.0f / mySampleRate * 1046.5f; // Phase is 101920256

  float cycle = 1046.5f;
  int phase = 4294967296.0f / mySampleRate * cycle; // Phase is 101920480 


Windows 10, VS 2019
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
   float mySampleRate = 44100.0f;

   int phase = 4294967296.0f / mySampleRate * 1046.5f; // Phase is 101920256

   std::cout << phase << '\n';

   float cycle = 1046.5f;

   phase = 4294967296.0f / mySampleRate * cycle; // Phase is 101920480

   std::cout << phase << '\n';
}

101920256
101920256

Change the floats to doubles and the output is:
101920255
101920255


Win10 Pro x64 and VS 2019 Community.
Code::Blocks gives the same results, floats or doubles. Some part of your code you didn't post is probably changing the value you are expecting.
Last edited on
Topic archived. No new replies allowed.