소피it블로그

[Swift 공식문서] The Basics 정리 (3) - Integers, Floating-Point Numbers 정수, 실수 본문

개발_iOS/스위프트

[Swift 공식문서] The Basics 정리 (3) - Integers, Floating-Point Numbers 정수, 실수

sophie_l 2022. 5. 10. 20:40

https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html

 

The Basics — The Swift Programming Language (Swift 5.6)

The Basics Swift is a new programming language for iOS, macOS, watchOS, and tvOS app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C. Swift provides its own versions of all fundamental

docs.swift.org

1. 정수

  • signed vs unsigned: signed는 음수, 양수, 0을 전부 포함, unsigned는 양수와 0만 해당하며 8, 16, 32, 64 비트 형식으로 제공된다.
  • min와 max 프라퍼티를 통해 각 정수 타입의 최댓값과 최솟값에 접근할 수 있다.
let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8
  • 웬만한 범위의 정수는 Int로 커버 가능하므로 Int 위주로 사용할 것
  • unsigned Int인 UInt라는 정수 타입도 존재하지만 반드시 사용해야 할 상황이 아닌 경우엔 Int를 사용하는 것이 일관성과 type safety 측면에서 더 바람직하다.

 

2. 실수

  • 정수 타입보다 훨씬 넓은 범위의 수를 나타낼 수 있으며, 정수 타입에 저장할 수 있는 것보다 훨씬 크거나 작은 수를 저장할 수 있다.
  • Double: 64비트 실수
  • Float: 32비트 실수
  • Double이 Float보다 정확성이 높다. 둘 다 사용 가능한 경우에는 Double이 더 좋은 선택이다.