objective-c 使用 CGFloat 和 float 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1264924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:03:33  来源:igfitidea点击:

What's the difference between using CGFloat and float?

iphoneobjective-ccocoa-touch

提问by Quinn Taylor

I tend to use CGFloat all over the place, but I wonder if I get a senseless "performance hit" with this. CGFloat seems to be something "heavier" than float, right? At which points should I use CGFloat, and what makes really the difference?

我倾向于在所有地方使用 CGFloat,但我想知道我是否会因此获得毫无意义的“性能打击”。CGFloat 似乎比 float 更“重”,对吧?我应该在哪些时候使用 CGFloat,真正的区别是什么?

回答by Quinn Taylor

As @weichsel stated, CGFloat is just a typedef for either floator double. You can see for yourself by Command-double-clicking on "CGFloat" in Xcode — it will jump to the CGBase.h header where the typedef is defined. The same approach is used for NSInteger and NSUInteger as well.

正如@weichsel 所说, CGFloat 只是float或 的类型定义double。您可以通过 Command 双击 Xcode 中的“CGFloat”亲眼看到——它会跳转到定义 typedef 的 CGBase.h 头文件。同样的方法也用于 NSInteger 和 NSUInteger。

These types were introduced to make it easier to write code that works on both 32-bit and 64-bit without modification. However, if all you need is floatprecision within your own code, you can still use floatif you like —?it will reduce your memory footprint somewhat. Same goes for integer values.

引入这些类型是为了更轻松地编写无需修改即可在 32 位和 64 位上运行的代码。但是,如果您只需要float自己代码中的精确度,您仍然可以float根据需要使用——它会在一定程度上减少您的内存占用。整数值也是如此。

I suggest you invest the modest time required to make your app 64-bit clean and try running it as such, since most Macs now have 64-bit CPUs and Snow Leopard is fully 64-bit, including the kernel and user applications. Apple's 64-bit Transition Guide for Cocoais a useful resource.

我建议您投入适量的时间使您的应用程序变得 64 位干净并尝试运行它,因为现在大多数 Mac 都有 64 位 CPU,而 Snow Leopard 是完全 64 位的,包括内核和用户应用程序。Apple 的Cocoa 64 位过渡指南是一个有用的资源。

回答by Thomas Zoechling

CGFloat is a regular float on 32-bit systems and a double on 64-bit systems

CGFloat 是 32 位系统上的常规浮点数和 64 位系统上的双精度数

typedef float CGFloat;// 32-bit
typedef double CGFloat;// 64-bit

So you won't get any performance penalty.

所以你不会得到任何性能损失。

回答by user3259383

As others have said, CGFloat is a float on 32-bit systems and a double on 64-bit systems. However, the decision to do that was inherited from OS X, where it was made based on the performance characteristics of early PowerPC CPUs. In other words, you should not think that float is for 32-bit CPUs and double is for 64-bit CPUs. (I believe, Apple's ARM processors were able to process doubles long before they went 64-bit.) The main performance hit of using doubles is that they use twice the memory and therefore might be slower if you are doing a lot of floating point operations.

正如其他人所说,CGFloat 在 32 位系统上是一个浮点数,在 64 位系统上是一个双精度数。然而,这样做的决定是从 OS X 继承而来的,它是根据早期 PowerPC CPU 的性能特征做出的。换句话说,您不应该认为 float 是针对 32 位 CPU 的,而 double 是针对 64 位 CPU 的。(我相信,Apple 的 ARM 处理器在进入 64 位之前就能够处理双精度数。)使用双精度数的主要性能影响是它们使用两倍的内存,因此如果您进行大量浮点运算,速度可能会更慢.

回答by Ben Leggiero

Objective-C

目标-C

From the Foundation source code, in CoreGraphics' CGBase.h:

来自 Foundation 源代码,在 CoreGraphics' 中CGBase.h

/* Definition of `CGFLOAT_TYPE', `CGFLOAT_IS_DOUBLE', `CGFLOAT_MIN', and
   `CGFLOAT_MAX'. */

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif

/* Definition of the `CGFloat' type and `CGFLOAT_DEFINED'. */

typedef CGFLOAT_TYPE CGFloat;
#define CGFLOAT_DEFINED 1

Copyright (c) 2000-2011 Apple Inc.

版权所有 (c) 2000-2011 Apple Inc.

This is essentially doing:

这基本上是在做:

#if defined(__LP64__) && __LP64__
typedef double CGFloat;
#else
typedef float CGFloat;
#endif

Where __LP64__indicates whether the current architecture* is 64-bit.

其中__LP64__指示当前架构*是否为 64 位。

Note that 32-bit systems can still use the 64-bit double, it just takes more processor time, so CoreGraphics does this for optimization purposes, not for compatibility. If you aren't concerned about performance but are concerned about accuracy, simply use double.

请注意,32 位系统仍然可以使用 64 位double,只是需要更多的处理器时间,因此 CoreGraphics 这样做是为了优化目的,而不是为了兼容性。如果您不关心性能但关心准确性,只需使用double.

Swift

迅速

In Swift, CGFloatis a structwrapper around either Floaton 32-bit architectures or Doubleon 64-bit ones (You can detect this at run- or compile-time with CGFloat.NativeType)

在 Swift 中,它CGFloat是32 位架构或64位架构的struct包装器(您可以在运行或编译时使用 来检测)FloatDoubleCGFloat.NativeType

From the CoreGraphics source code, in CGFloat.swift.gyb:

从 CoreGraphics 源代码中,CGFloat.swift.gyb

public struct CGFloat {
#if arch(i386) || arch(arm)
  /// The native type used to store the CGFloat, which is Float on
  /// 32-bit architectures and Double on 64-bit architectures.
  public typealias NativeType = Float
#elseif arch(x86_64) || arch(arm64)
  /// The native type used to store the CGFloat, which is Float on
  /// 32-bit architectures and Double on 64-bit architectures.
  public typealias NativeType = Double
#endif


*Specifically, longs and pointers, hence the LP. See also: http://www.unix.org/version2/whatsnew/lp64_wp.html

*特别是longs 和指针,因此LP. 另见:http: //www.unix.org/version2/whatsnew/lp64_wp.html

回答by Boyan Jakimov

just mention that - Jan, 2020 Xcode 11.3/iOS13

只是提一下 - 2020 年 1 月 Xcode 11.3/iOS13

Swift 5

斯威夫特 5

From the CoreGraphics source code

来自 CoreGraphics 源代码

public struct CGFloat {
    /// The native type used to store the CGFloat, which is Float on
    /// 32-bit architectures and Double on 64-bit architectures.
    public typealias NativeType = Double