Giter Site home page Giter Site logo

Comments (15)

ricardo-rendoncepeda avatar ricardo-rendoncepeda commented on July 4, 2024

This is very important in graphics applications, so I vote +1 on the suffix. Alternatively, we can leave it out and I can clarify it's importance in any of my OpenGL ES tutorials.

from objective-c-style-guide.

 avatar commented on July 4, 2024

I always use the f as I believe it acts as a hint to the compiler, as well as the coder.

Sent from my iPhone

On 8 Nov 2013, at 17:15, ColinEberhardt [email protected] wrote:

The current style guide does not detail whether we should or should not, but the 'f' suffix is lacking in one of the examples.

static const CGFloat RWImageThumbnailHeight = 50.0;
I am on the fence about this one.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

hollance avatar hollance commented on July 4, 2024

I always use them (except for things that are doubles, of course, such as NSTimeInterval).

But I've been wondering: does it make a difference on 64-bit?

from objective-c-style-guide.

pietrorea avatar pietrorea commented on July 4, 2024

I would add in the 'f' if only to hint to the reader that there's more than just integers and decimals to consider.

from objective-c-style-guide.

gregheo avatar gregheo commented on July 4, 2024

I don't think it's worthwhile. My understanding is that bare 10.0 is a double, and the point of 10.0f is to explicitly make it a float. float foo = 10.0; will convert down just fine but double foo = 10.0f; will lose precision. We're in C-based static type land so I say let the compiler convert or optimize as it sees fit.

Plus it just looks weird and it's redundant. The decimal .0 part already shows it to be a floating-point number.

from objective-c-style-guide.

moayes avatar moayes commented on July 4, 2024

I agree with @gregheo.

from objective-c-style-guide.

icanzilb avatar icanzilb commented on July 4, 2024

@gregheo Does that mean you're also fine with

float foo = 10; 

from objective-c-style-guide.

gregheo avatar gregheo commented on July 4, 2024

I write float foo = 10; all the time. Again, the type says it's a float so there's no strict need to say 10.0.

That said: for the style guide, I would stick with float foo = 10.0; (still without the f). That avoids the surprise of say float foo = 10 / 3; being 3 rather than 3.33333.

from objective-c-style-guide.

icanzilb avatar icanzilb commented on July 4, 2024

great, my point exactly

from objective-c-style-guide.

ndubbs avatar ndubbs commented on July 4, 2024

I agree that we should set the value like float foo = 10.0; instead of float foo = 10;.

Getting back to the original line of code, should there be any changes?
static const CGFloat RWImageThumbnailHeight = 50.0;

from objective-c-style-guide.

gregheo avatar gregheo commented on July 4, 2024

Small change (and just a small can of worms, I hope):

static CGFloat const RWImageThumbnailHeight = 50.0;

Rationale: I know both CGFloat const and const CGFloat work, but this fits the "read the type right-to-left" thing. More complicated when pointers are involved of course, as here:
http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const

But, objects are pointers so it's good to have an order that makes it easy to reason through NSString const * vs NSString * const.

from objective-c-style-guide.

hollance avatar hollance commented on July 4, 2024

It turns out that CGFloat is a double on 64-bit, so dropping the "f" suffix makes sense.

from objective-c-style-guide.

John-Lluch avatar John-Lluch commented on July 4, 2024

Just for those who think that an 'f' can be a hint or whatever to the compiler; It does not work like that. The three following statements will produce EXACTLY the same compiled code

float foo = 10.0;
float foo = 10.0f;
float foo = 10;

The compiler will just create a 'foo' variable with the float value 10.0f in it. There are no precision issues at all on any of the above. They produce identical code.

from objective-c-style-guide.

 avatar commented on July 4, 2024

It hails from C.

Floating point literals are double precision by default. Adding the f suffix informs the compiler to treat them as single precision.

Mic Pringle
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Wednesday, 15 January 2014 at 21:30, John Lluch Zorrilla wrote:

Just for those who think that an 'f' can be a hint or whatever to the compiler; It does not work like that. The three following statements will produce EXACTLY the same compiled code
float foo = 10.0;
float foo = 10.0f;
float foo = 10;
The compiler will just create a 'foo' variable with the float value 10.0f in it. There are no precision issues at all on any of the above. They produce identical code.


Reply to this email directly or view it on GitHub (#23 (comment)).

from objective-c-style-guide.

John-Lluch avatar John-Lluch commented on July 4, 2024

Hi, micpringle, my point was that for the single case of a declaration with an assignment, any of the above produces the same. This is because the compiler implicitly converts any of the them to a float (32 bits) at compile time, not at running time. In fact, any constant expression will be converted to a single value of the appropriate type at compile time. Said that, it is true that 10.0 is implicitly a double, but this only has an effect when used in an expression. For example 10.0/3 will not produce the same than 10.0f/3. The first one is 3.333... as a double and the second one is 3.333... as float. If you assign 10.0f/3 to a double you will loose precision.

from objective-c-style-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.