Float precision problem in metal shading language









up vote
0
down vote

favorite












I pass a float param with value 0.00291545 to fragment shader.
But fragment function got n/a .
swift code
fragment shader



In Xcode debugger's "Bound Resources" view, it shows that the value is 0.003
Bound Resources



In Xcode shader debugger view, I made some testes. It shows that 0.0004 will be casted to 0 and 0.0005 will be casted to 0.001 . The float value behaves like half value.
test code in shader debugger



So, my question is:



  1. Why 0.00291545 is first casted to 0.003, then casted to 0 ?


  2. Why float value has the same precision with half ?


  3. How could I use float value that has normal precision ?










share|improve this question





















  • Please post textual information (like source code) as text, not screenshots of text. Also, don't rely on the display of floating-point numbers by the debugger. It will generally round to just a limited number of significant digits to keep the display compact. That does not mean that the value was rounded in actuality, just in display. Make a test that actually checks the value in terms of the rendering result. Like draw all pixels green if the value is between 0.00291 and 0.00292 or red otherwise.
    – Ken Thomases
    Nov 10 at 20:07














up vote
0
down vote

favorite












I pass a float param with value 0.00291545 to fragment shader.
But fragment function got n/a .
swift code
fragment shader



In Xcode debugger's "Bound Resources" view, it shows that the value is 0.003
Bound Resources



In Xcode shader debugger view, I made some testes. It shows that 0.0004 will be casted to 0 and 0.0005 will be casted to 0.001 . The float value behaves like half value.
test code in shader debugger



So, my question is:



  1. Why 0.00291545 is first casted to 0.003, then casted to 0 ?


  2. Why float value has the same precision with half ?


  3. How could I use float value that has normal precision ?










share|improve this question





















  • Please post textual information (like source code) as text, not screenshots of text. Also, don't rely on the display of floating-point numbers by the debugger. It will generally round to just a limited number of significant digits to keep the display compact. That does not mean that the value was rounded in actuality, just in display. Make a test that actually checks the value in terms of the rendering result. Like draw all pixels green if the value is between 0.00291 and 0.00292 or red otherwise.
    – Ken Thomases
    Nov 10 at 20:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I pass a float param with value 0.00291545 to fragment shader.
But fragment function got n/a .
swift code
fragment shader



In Xcode debugger's "Bound Resources" view, it shows that the value is 0.003
Bound Resources



In Xcode shader debugger view, I made some testes. It shows that 0.0004 will be casted to 0 and 0.0005 will be casted to 0.001 . The float value behaves like half value.
test code in shader debugger



So, my question is:



  1. Why 0.00291545 is first casted to 0.003, then casted to 0 ?


  2. Why float value has the same precision with half ?


  3. How could I use float value that has normal precision ?










share|improve this question













I pass a float param with value 0.00291545 to fragment shader.
But fragment function got n/a .
swift code
fragment shader



In Xcode debugger's "Bound Resources" view, it shows that the value is 0.003
Bound Resources



In Xcode shader debugger view, I made some testes. It shows that 0.0004 will be casted to 0 and 0.0005 will be casted to 0.001 . The float value behaves like half value.
test code in shader debugger



So, my question is:



  1. Why 0.00291545 is first casted to 0.003, then casted to 0 ?


  2. Why float value has the same precision with half ?


  3. How could I use float value that has normal precision ?







ios swift precision metal shading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 17:57









blabla

33




33











  • Please post textual information (like source code) as text, not screenshots of text. Also, don't rely on the display of floating-point numbers by the debugger. It will generally round to just a limited number of significant digits to keep the display compact. That does not mean that the value was rounded in actuality, just in display. Make a test that actually checks the value in terms of the rendering result. Like draw all pixels green if the value is between 0.00291 and 0.00292 or red otherwise.
    – Ken Thomases
    Nov 10 at 20:07
















  • Please post textual information (like source code) as text, not screenshots of text. Also, don't rely on the display of floating-point numbers by the debugger. It will generally round to just a limited number of significant digits to keep the display compact. That does not mean that the value was rounded in actuality, just in display. Make a test that actually checks the value in terms of the rendering result. Like draw all pixels green if the value is between 0.00291 and 0.00292 or red otherwise.
    – Ken Thomases
    Nov 10 at 20:07















Please post textual information (like source code) as text, not screenshots of text. Also, don't rely on the display of floating-point numbers by the debugger. It will generally round to just a limited number of significant digits to keep the display compact. That does not mean that the value was rounded in actuality, just in display. Make a test that actually checks the value in terms of the rendering result. Like draw all pixels green if the value is between 0.00291 and 0.00292 or red otherwise.
– Ken Thomases
Nov 10 at 20:07




Please post textual information (like source code) as text, not screenshots of text. Also, don't rely on the display of floating-point numbers by the debugger. It will generally round to just a limited number of significant digits to keep the display compact. That does not mean that the value was rounded in actuality, just in display. Make a test that actually checks the value in terms of the rendering result. Like draw all pixels green if the value is between 0.00291 and 0.00292 or red otherwise.
– Ken Thomases
Nov 10 at 20:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I found what the problem was!



Xcode debugger misled me.



0.00291545 was not casted to 0.003 and was not casted to n/a. The debugger' display is not accurate, but the value is accurate actually.



Thanks for Ken Thomases






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%2f53241847%2ffloat-precision-problem-in-metal-shading-language%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I found what the problem was!



    Xcode debugger misled me.



    0.00291545 was not casted to 0.003 and was not casted to n/a. The debugger' display is not accurate, but the value is accurate actually.



    Thanks for Ken Thomases






    share|improve this answer
























      up vote
      0
      down vote













      I found what the problem was!



      Xcode debugger misled me.



      0.00291545 was not casted to 0.003 and was not casted to n/a. The debugger' display is not accurate, but the value is accurate actually.



      Thanks for Ken Thomases






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        I found what the problem was!



        Xcode debugger misled me.



        0.00291545 was not casted to 0.003 and was not casted to n/a. The debugger' display is not accurate, but the value is accurate actually.



        Thanks for Ken Thomases






        share|improve this answer












        I found what the problem was!



        Xcode debugger misled me.



        0.00291545 was not casted to 0.003 and was not casted to n/a. The debugger' display is not accurate, but the value is accurate actually.



        Thanks for Ken Thomases







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 11:42









        blabla

        33




        33



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241847%2ffloat-precision-problem-in-metal-shading-language%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto