Which platform and which JS engine is that?

There are several possible issues here. The JS engine might be using the
system libc pow, but it might have it's own implementation (e.g. sin and
cos have been implemented in browsers), and different libcs and so forth
can be more or less precise. Also, since there are floats here, building
with -s PRECISE_F32=1 can make the results more like native. With that I
get almost identical results, but still some tiny rounding errors. But even
without it, the errors are much smaller than yours.

- Alon



On Wed, Jan 28, 2015 at 1:43 PM, Stéphane Letz <[email protected]> wrote:

> The following code :
>
>
> double midiToFreq(double note)
>
> {
>
>     return 440.0 * pow(2.0, (note-69.0)/12.0);
>
> }
>
>
> float midiToFreq2(float note)
>
> {
>
>     return 440.0f * powf(2.0f, (note-69.0f)/12.0f);
>
> }
>
>
> int main()
>
> {
>
>     for (int i = 48; i < 84; i++) {
>
>         printf("pitch %d %f\n", i, midiToFreq(i));
>
>         printf("pitch %d %f\n", i, midiToFreq2(i));
>
>     }
>
> }
>
> correctly gives :
>
> pitch 48 130.812783
> pitch 48 130.812775
> pitch 49 138.591315
> pitch 49 138.591324
> pitch 50 146.832384
> pitch 50 146.832382
> pitch 51 155.563492
> pitch 51 155.563492
> pitch 52 164.813778
> pitch 52 164.813782
> pitch 53 174.614116
> ….
>
> when compiled with a C compiler and incorrectly gives the following when
> compiled with emcc and displayed in JS :
>
> pitch 48 130.812783
> pitch 48 130.812783
> pitch 49 138.591315
> pitch 49 136.604359
> pitch 50 146.832384
> pitch 50 148.968110
> pitch 51 155.563492
> pitch 51 155.563492
> pitch 52 164.813778
> pitch 52 162.450876
> pitch 53 174.614116
> pitch 53 177.153937
> pitch 54 184.997211
>
> When could be the reason for this strange behavior of the powf function ?
>
> Thanks.
>
> Stéphane Letz
>
>  --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to