Issue while calculating float value using NodeJS N-API











up vote
1
down vote

favorite












Hello everyone,



Coming from the web dev world. I am currently trying to do some C code which is converting an RGB value to an XYZ value that can be used by NodeJS through N-API. The issue that I have is regarding to float calculation. Below is the explanation of my issue:



Based on this C code below, this code is trying to convert an RGB value to an XYZ value.



char * colorType = getStringValue(env, funcParams[2], SPACELen);
// m is either the value srgb | adobeRgb
Matrix m = getEnumFromStr(colorType);
Rgb * rgb = getRGBFromJSObj(env, funcParams[0]);
xyz = generateXyzFromRgb(rgb, m);


And I am using this JS snippet to call my library



const rgb = {
r: 255,
g: 255,
b: 255
};

const xyz = lib.getXyzFromRgb(rgb, "srgb", 10000);
expect(xyz).to.be.deep.equal({
x: 0.9504,
y: 1,
z: 1.0888
});


If everything should be all right the output should be like the one below



{
x: 0.9504,
y: 1,
z: 1.0888
}


However the output that I have is this one



{
x: 0.9502,
y: 0.9997,
z: 1.0886
}


As you can see the output is totally wrong. However this wrong output only happened on my local machine (OSX) and only when I am trying to do the conversion by using the JS snippet.



Indeed, when I am trying to run the conversion with this piece of code below directly through Xcode the output is correct



// RGB and & m variable is outputing the same value as the conversion done by the C code above
xyz = generateXyzFromRgb(rgb, m);


Moreover when I'm trying to call the JS code through travis which also running OSX and on Ubuntu through Docker the JS code also output the right value.



Can it be more related to the hardware or the way I am compiling my libraries or else ?



Thank you in advance.










share|improve this question






















  • What are the numeric data types in the C code?
    – Pointy
    2 days ago










  • Also in general in any programming language where numbers are binary floating-point values, performing strict equality tests is tricky.
    – Pointy
    2 days ago










  • @Pointy the numeric data type is float. Should i change this to double ?. Indeed I looked at the IEE754 which looks to be tricky... However I find it strange that the wrong output only happened on my machine and only when using the JS snippet and not on travis osx or a in ubuntu
    – Yellowman
    2 days ago












  • The C float type is (usually) only 32 bits; JavaScript numbers are 64 bits.
    – Pointy
    2 days ago












  • @Pointy Hmm yes, behind the usage of the float value I am converting the float to the double value in order for the value to be used by N-API. Do you think that there might be a loss of precision during this conversion ? I guess I should try to convert it right away into double and see how it goes
    – Yellowman
    2 days ago

















up vote
1
down vote

favorite












Hello everyone,



Coming from the web dev world. I am currently trying to do some C code which is converting an RGB value to an XYZ value that can be used by NodeJS through N-API. The issue that I have is regarding to float calculation. Below is the explanation of my issue:



Based on this C code below, this code is trying to convert an RGB value to an XYZ value.



char * colorType = getStringValue(env, funcParams[2], SPACELen);
// m is either the value srgb | adobeRgb
Matrix m = getEnumFromStr(colorType);
Rgb * rgb = getRGBFromJSObj(env, funcParams[0]);
xyz = generateXyzFromRgb(rgb, m);


And I am using this JS snippet to call my library



const rgb = {
r: 255,
g: 255,
b: 255
};

const xyz = lib.getXyzFromRgb(rgb, "srgb", 10000);
expect(xyz).to.be.deep.equal({
x: 0.9504,
y: 1,
z: 1.0888
});


If everything should be all right the output should be like the one below



{
x: 0.9504,
y: 1,
z: 1.0888
}


However the output that I have is this one



{
x: 0.9502,
y: 0.9997,
z: 1.0886
}


As you can see the output is totally wrong. However this wrong output only happened on my local machine (OSX) and only when I am trying to do the conversion by using the JS snippet.



Indeed, when I am trying to run the conversion with this piece of code below directly through Xcode the output is correct



// RGB and & m variable is outputing the same value as the conversion done by the C code above
xyz = generateXyzFromRgb(rgb, m);


Moreover when I'm trying to call the JS code through travis which also running OSX and on Ubuntu through Docker the JS code also output the right value.



Can it be more related to the hardware or the way I am compiling my libraries or else ?



Thank you in advance.










share|improve this question






















  • What are the numeric data types in the C code?
    – Pointy
    2 days ago










  • Also in general in any programming language where numbers are binary floating-point values, performing strict equality tests is tricky.
    – Pointy
    2 days ago










  • @Pointy the numeric data type is float. Should i change this to double ?. Indeed I looked at the IEE754 which looks to be tricky... However I find it strange that the wrong output only happened on my machine and only when using the JS snippet and not on travis osx or a in ubuntu
    – Yellowman
    2 days ago












  • The C float type is (usually) only 32 bits; JavaScript numbers are 64 bits.
    – Pointy
    2 days ago












  • @Pointy Hmm yes, behind the usage of the float value I am converting the float to the double value in order for the value to be used by N-API. Do you think that there might be a loss of precision during this conversion ? I guess I should try to convert it right away into double and see how it goes
    – Yellowman
    2 days ago















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Hello everyone,



Coming from the web dev world. I am currently trying to do some C code which is converting an RGB value to an XYZ value that can be used by NodeJS through N-API. The issue that I have is regarding to float calculation. Below is the explanation of my issue:



Based on this C code below, this code is trying to convert an RGB value to an XYZ value.



char * colorType = getStringValue(env, funcParams[2], SPACELen);
// m is either the value srgb | adobeRgb
Matrix m = getEnumFromStr(colorType);
Rgb * rgb = getRGBFromJSObj(env, funcParams[0]);
xyz = generateXyzFromRgb(rgb, m);


And I am using this JS snippet to call my library



const rgb = {
r: 255,
g: 255,
b: 255
};

const xyz = lib.getXyzFromRgb(rgb, "srgb", 10000);
expect(xyz).to.be.deep.equal({
x: 0.9504,
y: 1,
z: 1.0888
});


If everything should be all right the output should be like the one below



{
x: 0.9504,
y: 1,
z: 1.0888
}


However the output that I have is this one



{
x: 0.9502,
y: 0.9997,
z: 1.0886
}


As you can see the output is totally wrong. However this wrong output only happened on my local machine (OSX) and only when I am trying to do the conversion by using the JS snippet.



Indeed, when I am trying to run the conversion with this piece of code below directly through Xcode the output is correct



// RGB and & m variable is outputing the same value as the conversion done by the C code above
xyz = generateXyzFromRgb(rgb, m);


Moreover when I'm trying to call the JS code through travis which also running OSX and on Ubuntu through Docker the JS code also output the right value.



Can it be more related to the hardware or the way I am compiling my libraries or else ?



Thank you in advance.










share|improve this question













Hello everyone,



Coming from the web dev world. I am currently trying to do some C code which is converting an RGB value to an XYZ value that can be used by NodeJS through N-API. The issue that I have is regarding to float calculation. Below is the explanation of my issue:



Based on this C code below, this code is trying to convert an RGB value to an XYZ value.



char * colorType = getStringValue(env, funcParams[2], SPACELen);
// m is either the value srgb | adobeRgb
Matrix m = getEnumFromStr(colorType);
Rgb * rgb = getRGBFromJSObj(env, funcParams[0]);
xyz = generateXyzFromRgb(rgb, m);


And I am using this JS snippet to call my library



const rgb = {
r: 255,
g: 255,
b: 255
};

const xyz = lib.getXyzFromRgb(rgb, "srgb", 10000);
expect(xyz).to.be.deep.equal({
x: 0.9504,
y: 1,
z: 1.0888
});


If everything should be all right the output should be like the one below



{
x: 0.9504,
y: 1,
z: 1.0888
}


However the output that I have is this one



{
x: 0.9502,
y: 0.9997,
z: 1.0886
}


As you can see the output is totally wrong. However this wrong output only happened on my local machine (OSX) and only when I am trying to do the conversion by using the JS snippet.



Indeed, when I am trying to run the conversion with this piece of code below directly through Xcode the output is correct



// RGB and & m variable is outputing the same value as the conversion done by the C code above
xyz = generateXyzFromRgb(rgb, m);


Moreover when I'm trying to call the JS code through travis which also running OSX and on Ubuntu through Docker the JS code also output the right value.



Can it be more related to the hardware or the way I am compiling my libraries or else ?



Thank you in advance.







javascript c node.js node.js-addon node.js-napi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Yellowman

213




213












  • What are the numeric data types in the C code?
    – Pointy
    2 days ago










  • Also in general in any programming language where numbers are binary floating-point values, performing strict equality tests is tricky.
    – Pointy
    2 days ago










  • @Pointy the numeric data type is float. Should i change this to double ?. Indeed I looked at the IEE754 which looks to be tricky... However I find it strange that the wrong output only happened on my machine and only when using the JS snippet and not on travis osx or a in ubuntu
    – Yellowman
    2 days ago












  • The C float type is (usually) only 32 bits; JavaScript numbers are 64 bits.
    – Pointy
    2 days ago












  • @Pointy Hmm yes, behind the usage of the float value I am converting the float to the double value in order for the value to be used by N-API. Do you think that there might be a loss of precision during this conversion ? I guess I should try to convert it right away into double and see how it goes
    – Yellowman
    2 days ago




















  • What are the numeric data types in the C code?
    – Pointy
    2 days ago










  • Also in general in any programming language where numbers are binary floating-point values, performing strict equality tests is tricky.
    – Pointy
    2 days ago










  • @Pointy the numeric data type is float. Should i change this to double ?. Indeed I looked at the IEE754 which looks to be tricky... However I find it strange that the wrong output only happened on my machine and only when using the JS snippet and not on travis osx or a in ubuntu
    – Yellowman
    2 days ago












  • The C float type is (usually) only 32 bits; JavaScript numbers are 64 bits.
    – Pointy
    2 days ago












  • @Pointy Hmm yes, behind the usage of the float value I am converting the float to the double value in order for the value to be used by N-API. Do you think that there might be a loss of precision during this conversion ? I guess I should try to convert it right away into double and see how it goes
    – Yellowman
    2 days ago


















What are the numeric data types in the C code?
– Pointy
2 days ago




What are the numeric data types in the C code?
– Pointy
2 days ago












Also in general in any programming language where numbers are binary floating-point values, performing strict equality tests is tricky.
– Pointy
2 days ago




Also in general in any programming language where numbers are binary floating-point values, performing strict equality tests is tricky.
– Pointy
2 days ago












@Pointy the numeric data type is float. Should i change this to double ?. Indeed I looked at the IEE754 which looks to be tricky... However I find it strange that the wrong output only happened on my machine and only when using the JS snippet and not on travis osx or a in ubuntu
– Yellowman
2 days ago






@Pointy the numeric data type is float. Should i change this to double ?. Indeed I looked at the IEE754 which looks to be tricky... However I find it strange that the wrong output only happened on my machine and only when using the JS snippet and not on travis osx or a in ubuntu
– Yellowman
2 days ago














The C float type is (usually) only 32 bits; JavaScript numbers are 64 bits.
– Pointy
2 days ago






The C float type is (usually) only 32 bits; JavaScript numbers are 64 bits.
– Pointy
2 days ago














@Pointy Hmm yes, behind the usage of the float value I am converting the float to the double value in order for the value to be used by N-API. Do you think that there might be a loss of precision during this conversion ? I guess I should try to convert it right away into double and see how it goes
– Yellowman
2 days ago






@Pointy Hmm yes, behind the usage of the float value I am converting the float to the double value in order for the value to be used by N-API. Do you think that there might be a loss of precision during this conversion ? I guess I should try to convert it right away into double and see how it goes
– Yellowman
2 days ago














2 Answers
2






active

oldest

votes

















up vote
2
down vote













In javascript v8 engine. Actually only exist smi and double.
readFloatBE: when float pass to v8, the float will be cast to double.
it will promote to the double when you go out and rounding into a float.



First, if you really want to get value like C, you need to manually specify the rounded digits and re-instantiate the Number object with the Number.prototype.toPrecision function rounded up:



For your reference:



var v = 5.2
var buffer = new Buffer(5)
buffer.writeFloatBE(v)
var g = buffer.readFloatBE()

console.log(v)
console.log(g)
console.log(v==g)
console.log(Number(g.toPrecision(5)))





share|improve this answer








New contributor




Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
    – Yellowman
    yesterday


















up vote
0
down vote



accepted










Indeed, as post by Pointy and user10683038 JavaScript numbers are 64 bits.



As I coded this feature the numeric type used was float (was not thinking at making a Node module at first glance). Later on I decided to do a conversion from float to double using a small util method like below



double v = *(double *) arg;
status = napi_create_double(env, v, &value);


Strangely it appear that I might have randomly loosing decimal precision while doing this conversion while it should have not (32bit to 64bit) or I might have missing out something.



Later on I refactor the same code but this time by using double instead and it works as it should. Many thanks to all of you.



Note: As I used this util code above method accross the NodeJS binding codebase I am surprised that I haven't got a decimal precision error earlier.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402705%2fissue-while-calculating-float-value-using-nodejs-n-api%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    In javascript v8 engine. Actually only exist smi and double.
    readFloatBE: when float pass to v8, the float will be cast to double.
    it will promote to the double when you go out and rounding into a float.



    First, if you really want to get value like C, you need to manually specify the rounded digits and re-instantiate the Number object with the Number.prototype.toPrecision function rounded up:



    For your reference:



    var v = 5.2
    var buffer = new Buffer(5)
    buffer.writeFloatBE(v)
    var g = buffer.readFloatBE()

    console.log(v)
    console.log(g)
    console.log(v==g)
    console.log(Number(g.toPrecision(5)))





    share|improve this answer








    New contributor




    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
      – Yellowman
      yesterday















    up vote
    2
    down vote













    In javascript v8 engine. Actually only exist smi and double.
    readFloatBE: when float pass to v8, the float will be cast to double.
    it will promote to the double when you go out and rounding into a float.



    First, if you really want to get value like C, you need to manually specify the rounded digits and re-instantiate the Number object with the Number.prototype.toPrecision function rounded up:



    For your reference:



    var v = 5.2
    var buffer = new Buffer(5)
    buffer.writeFloatBE(v)
    var g = buffer.readFloatBE()

    console.log(v)
    console.log(g)
    console.log(v==g)
    console.log(Number(g.toPrecision(5)))





    share|improve this answer








    New contributor




    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
      – Yellowman
      yesterday













    up vote
    2
    down vote










    up vote
    2
    down vote









    In javascript v8 engine. Actually only exist smi and double.
    readFloatBE: when float pass to v8, the float will be cast to double.
    it will promote to the double when you go out and rounding into a float.



    First, if you really want to get value like C, you need to manually specify the rounded digits and re-instantiate the Number object with the Number.prototype.toPrecision function rounded up:



    For your reference:



    var v = 5.2
    var buffer = new Buffer(5)
    buffer.writeFloatBE(v)
    var g = buffer.readFloatBE()

    console.log(v)
    console.log(g)
    console.log(v==g)
    console.log(Number(g.toPrecision(5)))





    share|improve this answer








    New contributor




    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    In javascript v8 engine. Actually only exist smi and double.
    readFloatBE: when float pass to v8, the float will be cast to double.
    it will promote to the double when you go out and rounding into a float.



    First, if you really want to get value like C, you need to manually specify the rounded digits and re-instantiate the Number object with the Number.prototype.toPrecision function rounded up:



    For your reference:



    var v = 5.2
    var buffer = new Buffer(5)
    buffer.writeFloatBE(v)
    var g = buffer.readFloatBE()

    console.log(v)
    console.log(g)
    console.log(v==g)
    console.log(Number(g.toPrecision(5)))






    share|improve this answer








    New contributor




    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer






    New contributor




    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered 2 days ago









    Jack

    211




    211




    New contributor




    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    Jack is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.












    • Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
      – Yellowman
      yesterday


















    • Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
      – Yellowman
      yesterday
















    Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
    – Yellowman
    yesterday




    Hmm indeed. I could return a buffer instead of a float value that might help in term of precision
    – Yellowman
    yesterday












    up vote
    0
    down vote



    accepted










    Indeed, as post by Pointy and user10683038 JavaScript numbers are 64 bits.



    As I coded this feature the numeric type used was float (was not thinking at making a Node module at first glance). Later on I decided to do a conversion from float to double using a small util method like below



    double v = *(double *) arg;
    status = napi_create_double(env, v, &value);


    Strangely it appear that I might have randomly loosing decimal precision while doing this conversion while it should have not (32bit to 64bit) or I might have missing out something.



    Later on I refactor the same code but this time by using double instead and it works as it should. Many thanks to all of you.



    Note: As I used this util code above method accross the NodeJS binding codebase I am surprised that I haven't got a decimal precision error earlier.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Indeed, as post by Pointy and user10683038 JavaScript numbers are 64 bits.



      As I coded this feature the numeric type used was float (was not thinking at making a Node module at first glance). Later on I decided to do a conversion from float to double using a small util method like below



      double v = *(double *) arg;
      status = napi_create_double(env, v, &value);


      Strangely it appear that I might have randomly loosing decimal precision while doing this conversion while it should have not (32bit to 64bit) or I might have missing out something.



      Later on I refactor the same code but this time by using double instead and it works as it should. Many thanks to all of you.



      Note: As I used this util code above method accross the NodeJS binding codebase I am surprised that I haven't got a decimal precision error earlier.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Indeed, as post by Pointy and user10683038 JavaScript numbers are 64 bits.



        As I coded this feature the numeric type used was float (was not thinking at making a Node module at first glance). Later on I decided to do a conversion from float to double using a small util method like below



        double v = *(double *) arg;
        status = napi_create_double(env, v, &value);


        Strangely it appear that I might have randomly loosing decimal precision while doing this conversion while it should have not (32bit to 64bit) or I might have missing out something.



        Later on I refactor the same code but this time by using double instead and it works as it should. Many thanks to all of you.



        Note: As I used this util code above method accross the NodeJS binding codebase I am surprised that I haven't got a decimal precision error earlier.






        share|improve this answer












        Indeed, as post by Pointy and user10683038 JavaScript numbers are 64 bits.



        As I coded this feature the numeric type used was float (was not thinking at making a Node module at first glance). Later on I decided to do a conversion from float to double using a small util method like below



        double v = *(double *) arg;
        status = napi_create_double(env, v, &value);


        Strangely it appear that I might have randomly loosing decimal precision while doing this conversion while it should have not (32bit to 64bit) or I might have missing out something.



        Later on I refactor the same code but this time by using double instead and it works as it should. Many thanks to all of you.



        Note: As I used this util code above method accross the NodeJS binding codebase I am surprised that I haven't got a decimal precision error earlier.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Yellowman

        213




        213






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402705%2fissue-while-calculating-float-value-using-nodejs-n-api%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

            Sphinx de Gizeh