char note_array[] = "G#5";
int approx_freq = 880;
int result = 0;
if(note_array[1] == '#')
approx_freq = round(approx_freq * pow(2, 1.0/12));
switch(note_array[0])
{
case 'A': result = approx_freq;
break;
case 'B': result = round(approx_freq * pow(2, 2.0/12));
break;
case 'C': result = round(approx_freq / pow(2, 9.0/12));
break;
case 'D': result = round(approx_freq / pow(2, 7.0/12));
break;
case 'E': result = round(approx_freq / pow(2, 5.0/12));
break;
case 'F': result = round(approx_freq / pow(2, 4.0/12));
break;
case 'G': result = round(approx_freq / pow(2.0, 2.0/12));
break;
default: return 1;
break;
}
return result;
Output Coming - 830
Output Needed - 831
I know there is something wrong with my usage of the round function, but I don't know what I'm doing wrong. Can anybody please help me out?
You're rounding down when you do this step:
The exact value is
932.3275230361799
. This setsapprox_freq
to932
. Then when you do:approx_freq / pow(2.0, 2.0/12))
is830.3176053067962
, which rounds to830
.You shouldn't do rounding in your intermediate steps. Declare
approx_freq
to befloat
ordouble
, and only round the final result. Then the last calculation will result in830.609
, which rounds up to831
.