cgfloat

Round up a CGFloat in Swift

一个人想着一个人 提交于 2019-11-27 18:26:38
How can I round up a CGFloat in Swift? I've tried ceil(CDouble(myCGFloat)) but that only works on iPad Air & iPhone 5S. When running on another simulated device I get an error saying 'NSNumber' is not a subtype of 'CGFloat' Update : Apple have now defined some CGFloat-specific versions of common functions like ceil : func ceil(x: CGFloat) -> CGFloat ...specifically to cope with the 32/64-bit difference. If you simply use ceil with a CGFloat argument it should now work on all architectures. My original answer: This is pretty horrible, I think, but can anyone think of a better way? #if doesn't

CGFloat addition bug?

为君一笑 提交于 2019-11-27 08:11:03
问题 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 below) and this printed the same wrong value. CGFloat arr[3] = {34484000,512085280,143011440}; CGFloat sum = 0.0; sum = arr[0] + arr[1] + arr[2]; NSLog(@"%f",sum); int arr1[3] = {34484000,512085280,143011440}; int sum1 = 0.0; sum1 = arr1[0] + arr1

Swift expression was too complex to be solved in reasonable time

旧时模样 提交于 2019-11-27 07:46:39
问题 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: CGFloat) -> CGFloat { return CGFloat(Float(arc4random()/0xFFFFFFFF) * (max - min) + min) } 回答1: Why not reduce the complexity for the compiler by breaking the expression down into two sub-expressions? static func random(min: CGFloat, max: CGFloat) ->

Float is 0 after integer division

£可爱£侵袭症+ 提交于 2019-11-27 03:15:40
问题 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 for decimal places? I need to preserve the decimal result. Thanks Shani 回答1: The reason your code doesn't work is that you're dividing an integer by another integer and then casting the result to a float. So you have 2 (an integer) and some other

Swift random float between 0 and 1

谁说胖子不能爱 提交于 2019-11-26 21:34:56
In Swift, I'm trying to get a random float between 0 and 1 but I can't seem to get the type conversions to work. func randomCGFloat() -> CGFloat { return CGFloat(arc4random()) / UINT32_MAX } I'm getting a 'CGFloat' is not convertible to 'UInt8' error Running Xcode 6. Try initializing the divisor as a float as well, a la: CGFloat(Float(arc4random()) / Float(UINT32_MAX)) This is extension for random numbers of Int, Double, Float, CGFloat Swift 3 & 4 syntax import Foundation import CoreGraphics // MARK: Int Extension public extension Int { /// Returns a random Int point number between 0 and Int

How to round CGFloat

一个人想着一个人 提交于 2019-11-26 20:36:16
问题 I made this method + (CGFloat) round: (CGFloat)f { int a = f; CGFloat b = a; return b; } It works as expected but it only rounds down. And if it's a negative number it still rounds down. This was just a quick method I made, it isn't very important that it rounds correctly, I just made it to round the camera's x and y values for my game. Is this method okay? Is it fast? Or is there a better solution? 回答1: There are already standard functions with behaviors you might need in <math.h> such as: