Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,227 questions

56,129 answers

573 users

How to calculates the distance between two decimal numbers (absolute difference) in Swift

1 Answer

0 votes
import Foundation

// Function that returns the distance between two decimal numbers
func distanceBetween(_ a: Double, _ b: Double) -> Double {
    // The distance is the absolute value of the difference
    return abs(a - b)
}

// Test values
let x1: Double = 100.0
let y1: Double = 45.0

let x2: Double = 100.0
let y2: Double = -15.0

let x3: Double = -100.0
let y3: Double = -125.0

let x4: Double = -600.0
let y4: Double = 100.0

// Print results
print("Distance between \(x1) and \(y1) = \(distanceBetween(x1, y1))")
print("Distance between \(x2) and \(y2) = \(distanceBetween(x2, y2))")
print("Distance between \(x3) and \(y3) = \(distanceBetween(x3, y3))")
print("Distance between \(x4) and \(y4) = \(distanceBetween(x4, y4))")



/*
run:

Distance between 100.0 and 45.0 = 55.0
Distance between 100.0 and -15.0 = 115.0
Distance between -100.0 and -125.0 = 25.0
Distance between -600.0 and 100.0 = 700.0

*/

 



answered Jun 28 by avibootz

Related questions

...