Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

any significant difference

  • 07-03-2019 06:03PM
    #1
    Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭


    Feel a bit silly asking in a new thread but just wondering if there is any significance in declaring a float like this:

    float whatever = 0.f;
    or
    float whatever = 0.0f;

    besides the dropped zero after the decimal is there any advantage to doing the first?


Comments

  • Registered Users, Registered Users 2 Posts: 11,087 ✭✭✭✭28064212


    Stating the language would help. In C#, the former is a syntax error. Assuming it's Java, you could just do this to test:
    float x = 0.F;
    float y = 0.0F;
    System.out.print(x == y);
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭iLikeWaffles


    28064212 wrote: »
    Stating the language would help. In C#, the former is a syntax error. Assuming it's Java, you could just do this to test:
    float x = 0.F;
    float y = 0.0F;
    System.out.print(x == y);
    

    It's C++


  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    There isn't a difference no.


  • Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭iLikeWaffles


    I'm just thinking out loud here and a little busy porting code to set up a project.
    Just to followup. Lets say I have:
    #define pi   3.14159265358979323
    
    float 2pi = 0.00f;
    
    2pi = 2*pi;
    

    Would that make
    2pi == 6.28
    
    or
    2pi == 6.28318530717958646
    


  • Registered Users, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    You can just test these things for yourself!!

    Why would it round the float to two decimal places???


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭iLikeWaffles


    You can just test these things for yourself!!

    Why would it round the float to two decimal places???

    As I said I'm a little busy and thinking out loud. Why have a forum if that's the case???


  • Moderators, Society & Culture Moderators, Paid Member Posts: 16,077 Mod ✭✭✭✭smacl


    I'm just thinking out loud here and a little busy porting code to set up a project.
    Just to followup. Lets say I have:
    #define pi   3.14159265358979323
    
    float 2pi = 0.00f;
    
    2pi = 2*pi;
    

    Would that make
    2pi == 6.28
    
    or
    2pi == 6.28318530717958646
    

    Neither.

    Edit:

    1) 2pi is not a legal variable name in C++
    2) If you were to use a legal variable name, e.g. TwoPi the value would be 6.28318548 which is limited by that fact that a float is only 4 bytes
    3) You should avoid == equality checks on doubles and floats in C++ for values other than zero, see https://stackoverflow.com/questions/18971533/c-comparison-of-two-double-values-not-working-properly Use an epsilon value or similar.


Advertisement