Geek Culture
Published in

Geek Culture

Don’t get so worked up over Integers. You only need basic math!

Integers and Floating Points

Integers, UIntegers, Floating-Points, Decimals, and Doubles

Many people are afraid to learn how to code because they think a lot of “Math” is involved. But to let you in on a bit of secret, it just depends on what you are trying to do. We only need to know the basics like addition, subtraction, division, multiplication, and sometimes PEMDAS (Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction). Like every other language out there, Swift offers built-in methods to make our life easier think of it as using a calculator, except you are coding it! Before we jump into our next topic and talk about integers and floating-point numbers! If you have not checked out the previous article about Constants and Variables you should!

What are Integers?

Integers are essentially whole numbers that allow for positive values, negative values, or zero.

100
0
-2
1300400400

We also have an option for UInt (Unassigned Integers). These are values of either positive or zero, for example:

40
23
27
954
0

What are these “Bit Forms”?

Well, swift provides either signed or unsigned integers in 8, 16, 32, 64-bit forms. You are probably thinking, “what the heck is this?” Well, the naming convention is similar to the C language. Each type has a specific range that you can work within, and the way we declare these types are with the ones listed:

TYPES | VALUE RANGE
— — — — — — — — — -

Int8 | -128–127
Int16 | -32768–32767
Int32 | -2147483648–2147483647
Int64 | -9223372036854775808–9223372036854775807
UInt8 | 0–255
UInt16 | 0–65535
UInt32 | 0–4294967295
Uint64 | 0–18446744073709551615

Do I need to declare these types every time?

No, you don’t need to declare them. Instead, Swift offers us a default type that we can declare using “Int.” With int comes a default value range of 32-Bit like:

-2,147,483,648 
2,147,483,647

If you like a larger number value range, you need to declare Int64 as the value type.

What is a Floating-point number?

A floating-point number is a decimal or fractional component. This can be a positive or negative number such as:

3.14159
0.69
-124.41
421.12

The Float, Double, and Decimal variable types are different in the way that they store values. The main difference between the three is the precision. Float is a single-precision (32-bit) floating-point data type. Double is a double-precision (64-bit) floating-point data type. Finally, decimal is a (128-bit) floating-point data type. Besides the precision, another critical difference between the three is the storing of a value. Floats and Doubles are binary floating-point types. Decimals store their values in a decimal point type; because of this, decimals have much higher precision and are usually used for financial applications.

When to use a Float, Double, or Decimal?

Double type is the most used data type for real values except handling money. As for decimal, it’s best to use this type for a high level of accuracy and easy to avoid rounding errors. Finally, the float is used primarily for graphical libraries for its processing power, and it can endure rounding errors.

How to declare a Float, Double, or Decimal?

To declare a Float, Double, or Decimal, you have two options. The first option is to declare the type when you make your variable or constant. The second option is to allow swift to infer what type you are using automatically.

var pi: Double = 3.14159265359
var pi2 = 3.14159
let myAge: Float = 27.5var hotDog: Decimal = 3.50

If you don’t specify the type when declaring the variable, Swift will reference it as a Double type.

Conclusion!

I hope that you appreci-eight this article on “Integers and Floating-Points” 🎉We went over a few exciting topics in this article like:

• What are Integers
• What are Bit Forms
• When to declare these types
• What is a Floating-point number
• When to use a Float, Decimal, Double

In our next article, join me as we discuss Booleans!

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

No wonder Digital Twin is changing the world. Let’s understand what lies beneath.

Generations of Test Automation Frameworks

Enemy Destroyed Effect

iOS Keychain and LocalAuthentication Tutorial

Flutter UI Kit Logistics, Road To Bootstrapping A Studio

Buffer underun: My not stack overflow

T-SQL: How To Use ROW NUMBER() and Why

Why DevOps in Salesforce?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aaron Cleveland

Aaron Cleveland

iOS Developer @Eulerity | Father | Inspiring Mobile Developer

More from Medium

Difference between Print() and debugPrint(), Improve the performance of the App by log the data in…

SharePlay Tutorial — Share custom data between iOS and MacOS

Swift Essentials | Enums

How to Create Your Own iOS Password Generator