How to get the hash value of a double in Swift

Overview

In Swift, the hashValue method returns the hash value of a double.

A hash value, or a hash, is a unique value that differentiates an object’s value from others.

Note: Hash values are not the same for every execution of our program.

Syntax

doubleValue.hashValue

Parameter

  • doubleValue: This is the double value whose hash value we want.

Return value

This method returns the hash value of the double value.

Code example

// Create some double values
let d1 = 12.44
let d2 = 1.23333
let d3 = 100.1
let d4 = 1.0
// Print the exponents
print(d1.hashValue)
print(d2.hashValue)
print(d3.hashValue)
print(d4.hashValue)

Explanation

  • Lines 2 to 5: We create some double values.
  • Lines 8 to 11: We get the hash values of the double values we created. Then, we print them to the console.

Free Resources