Questions tagged [cgfloat]

A CGFloat is the basic CoreGraphics type for all floating-point values in the Cocoa and Cocoa Touch UI frameworks.

A CGFloat is the basic CoreGraphics type for all floating-point values in the Cocoa and Cocoa Touch UI frameworks.

CGFloat is defined in the CGGeometry Reference of CoreGraphics, which defines structures for geometric primitives and functions that operate on them. Depending on your platform, a CGFloat is either 32 or 64 bits.

Use this tag when asking questions on CGFloat value types themselves, or CoreGraphics functions which use them.

207 questions
17
votes
5 answers

Swift binary operator '+' cannot be applied to two CGFloat operands

I am writing a function in Swift to detect which hexagon I am clicking on. But I ran into a peculiar error message stating that I cannot add two CGFloats. Whatever I did, e.g. changing let to var, declare and assign separately, did not work. I guess…
Colliot
  • 1,522
  • 3
  • 16
  • 29
16
votes
2 answers

Does const go before or after CGFloat?

Does it even matter? Const before or const after? I'm guessing that whether I put const before or after CGFloat it makes the value of CGFloat constant, but what about the pointer? Is this right for Objective-C: // Example.h extern CGFloat const…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
14
votes
4 answers

Float is 0 after integer division

It might be a simple solution but I can not fix it. I am dividing 2 integers : finishedGameFinalScore = [score integerValue]; CGFloat interval = 2/finishedGameFinalScore; NSLog(@"interval = %f",interval); The log returns 0.000000 Is there a limit…
shannoga
  • 19,649
  • 20
  • 104
  • 169
11
votes
2 answers

CGFloat: round, floor, abs, and 32/64 bit precision

TLDR: How do I call standard floating point code in a way that compiles both 32 and 64 bit CGFloats without warnings? CGFloat is defined as either double or float, depending on the compiler settings and platform. I'm trying to write code that works…
leecbaker
  • 3,611
  • 2
  • 35
  • 51
11
votes
1 answer

Replacement for `fabs`, `fmax`, etc. for use with CGFloat on 64-bit iOS devices

Question Consider layout code like this: CGFloat descriptionHeight = // height of a paragraph of text with zero or more words; CGFloat imageHeight = // height of an image; CGFloat yCoordinateOfSomeOtherView = fmax(descriptionHeight, imageHeight) +…
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
11
votes
2 answers

NSNumber from CGFloat

Is there a safe way to "convert" a CGFloat to a NSNumber ? NSNumber has the numberWithFloat: and numberWithDouble: methods but CGFloat being defined as float or double depending on the platform, it seems risky to use either one of them. Or is…
Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
10
votes
4 answers

Confusion due to Swift lacking implicit conversion of CGFloat

Trying to do arithmetic in a function that returns `CGFloat, I get an error: Couldn't find overload for '/' that accepts supplied arguments func kDCControlDegreesToRadians(x : CGFloat) -> CGFloat { return (M_PI * (x) / 180.0) // error is…
Mani
  • 17,549
  • 13
  • 79
  • 100
7
votes
1 answer

Swift expression was too complex to be solved in reasonable time

I'm having an error when compiling a project in Xcode, it says: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions here's the code: static func random(min: CGFloat, max:…
lagro23
  • 123
  • 1
  • 6
7
votes
1 answer

'CGFloat' is not Convertible to 'Float' AND MORE

I have more problems within my app. My errors is as the title says: Swift Compiler Error. Error1: 'CGFloat' is not Convertible to 'Float' Code for the 1st and 2nd error: self.setPositionRelativeBot(pipeBot, x: xx, y:…
StigPing
  • 197
  • 13
6
votes
2 answers

Conversion between CGFloat and NSNumber without unnecessary promotion to Double

As we all know, CGFloat (which is ubiquitous in CoreGraphics, UIKit etc) can be a 32-bit or 64-bit floating point number, depending on the processor architecture. In C, CGFloat it is a typealias to float or double, in Swift is it defined as a struct…
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
6
votes
1 answer

Swift equivalent of ceilf for CGFloat

I was trying to do something like this var myCGFloat: CGFloat = 3.001 ceilf(myCGFloat) but ceilf only takes Float values. While searching around there were lots of different answers about doing this conversion or that depending on whether it is 32…
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
6
votes
2 answers

Accessing global const CGFloat defined in an Objective-c .m file from Swift

I have defined some constants in my .m files that I need to access form my swift code. They are defined: const CGFloat testValue = 40.0; and in my other objective-c .m files I can access them by using extern: extern const CGFloat testValue Is…
reza23
  • 3,079
  • 2
  • 27
  • 42
6
votes
3 answers

How can I distinguish between an unset float and one with a value of 0?

I have a method that needs to do a different thing when given an unset float than a float with the value of 0. Basically, I need to check whether or not a variable has been, counting it as set if it has a value of 0. So, what placeholder should I…
Nathan
  • 6,772
  • 11
  • 38
  • 55
5
votes
1 answer

CGFloat addition bug?

I was trying to add some CGFloat values recursively in my program. And I just realized in one particular scenario the total generated was incorrect. To ensure I had nothing wrong in my program logic, I created a simple example of that scenario (see…
Murali Raghuram
  • 255
  • 2
  • 8
5
votes
3 answers

What's the difference between using CGSizeMake and CGSize? Is one better than the other?

CGSize(width: 360, height: 480) and CGSizeMake(360, 480) seem to have the same effect. Is one preferred to the other? What is the difference?
Crashalot
  • 33,605
  • 61
  • 269
  • 439
1
2
3
13 14