From bc0eb1204f8259d1c3e348533e24f6bb2f697251 Mon Sep 17 00:00:00 2001 From: Seivan Heidari Date: Wed, 2 Jul 2014 15:02:33 +0200 Subject: [PATCH 1/3] Remove all code. Allow for conversion instead. Also use Global math functions (Since they now do implicit casts) --- ScalarArithmetic/ScalarArithmetic.swift | 216 +----------------- .../TestsAndSample.xcodeproj/project.pbxproj | 8 - .../TestsCGFloatFunctions.swift | 76 ------ .../TestsDoubleFunctions.swift | 78 ------- 4 files changed, 12 insertions(+), 366 deletions(-) delete mode 100644 TestsAndSample/TestsAndSampleTests/TestsCGFloatFunctions.swift delete mode 100644 TestsAndSample/TestsAndSampleTests/TestsDoubleFunctions.swift diff --git a/ScalarArithmetic/ScalarArithmetic.swift b/ScalarArithmetic/ScalarArithmetic.swift index 72034bc..15653a9 100644 --- a/ScalarArithmetic/ScalarArithmetic.swift +++ b/ScalarArithmetic/ScalarArithmetic.swift @@ -1,214 +1,22 @@ -import Darwin -import CoreGraphics -import Foundation - -protocol ScalarFunctions { - var acos:Double {get} - var asin:Double {get} - var atan:Double {get} - func atan2(x:Double) -> Double - var cos:Double {get} - var sin:Double {get} - var tan:Double {get} - var exp:Double {get} - var exp2:Double {get} - var log:Double {get} - var log10:Double {get} - var log2:Double {get} - func pow(exponent:Double) -> Double - var sqrt:Double {get} -} - - -extension Double : ScalarFunctions { - var abs:Double { return Double.abs(self) } - var acos:Double { return Darwin.acos(self) } - var asin:Double { return Darwin.asin(self) } - var atan:Double { return Darwin.atan(self) } - func atan2(x:Double) -> Double { return Darwin.atan2(self,x) } - var cos:Double { return Darwin.cos(self) } - var sin:Double { return Darwin.sin(self) } - var tan:Double { return Darwin.tan(self) } - var exp:Double { return Darwin.exp(self) } - var exp2:Double { return Darwin.exp2(self) } - var log:Double { return Darwin.log(self) } - var log10:Double{ return Darwin.log10(self) } - var log2:Double { return Darwin.log2(self) } - func pow(exponent:Double)-> Double { return Darwin.pow(self, exponent) } - var sqrt:Double { return Darwin.sqrt(self) } -} - -protocol ScalarArithmetic { - var toDouble:Double { get } - init(_ value:Double) -} - -extension Int : ScalarArithmetic { - var toDouble:Double { return Double(self) } -} - - -#if !(arch(x86_64) || arch(arm64)) -extension CGFloat : ScalarArithmetic, ScalarFunctions { - var toDouble:Double { return Double(self) } - var abs:Double { return Double(self).abs } - var acos:Double { return Double(self).acos } - var asin:Double { return Double(self).asin } - var atan:Double { return Double(self).atan } - func atan2(x:Double) -> Double { return Double(self).atan2(x) } - var cos:Double { return Double(self).cos } - var sin:Double { return Double(self).sin } - var tan:Double { return Double(self).tan } - var exp:Double { return Double(self).exp } - var exp2:Double { return Double(self).exp2 } - var log:Double { return Double(self).log } - var log10:Double { return Double(self).log10} - var log2:Double { return Double(self).log2 } - func pow(exponent:Double)-> Double { return Double(self).pow(exponent) } - var sqrt:Double { return Double(self).sqrt } -} - -#endif - - -//Equality T<===>T -//@infix func == (lhs:T,rhs:U) -> Bool { -// return (lhs.toDouble == rhs.toDouble) -//} -//@infix func != (lhs:T,rhs:U) -> Bool { -// return (lhs == rhs) == false -//} -@infix func <= (lhs:T,rhs:U) -> Bool { - return (lhs.toDouble <= rhs.toDouble) -} -@infix func < (lhs:T,rhs:U) -> Bool { - return (lhs.toDouble < rhs.toDouble) -} -@infix func >= (lhs:T,rhs:U) -> Bool { - return (lhs < rhs) == false -} -@infix func > (lhs:T,rhs:U) -> Bool { - return (lhs <= rhs) == false -} - -//Equality Double<==>T -//@infix func == (lhs:Double, rhs:T) -> Bool { -// return (lhs == rhs.toDouble) -//} -//@infix func != (lhs:Double, rhs:T) -> Bool { -// return (lhs == rhs) == false -//} -@infix func <= (lhs:Double, rhs:T) -> Bool { - return (lhs <= rhs.toDouble) -} -@infix func < (lhs:Double, rhs:T) -> Bool { - return (lhs < rhs.toDouble) -} -@infix func >= (lhs:Double, rhs:T) -> Bool { - return (lhs < rhs) == false -} -@infix func > (lhs:Double, rhs:T) -> Bool { - return (lhs <= rhs) == false -} - - -//Equality T<==>Double -//@infix func == (lhs:T,rhs:Double) -> Bool { -// return (lhs.toDouble == rhs) -//} -//@infix func != (lhs:T,rhs:Double) -> Bool { -// return (lhs == rhs) == false -//} -@infix func <= (lhs:T,rhs:Double) -> Bool { - return (lhs.toDouble <= rhs) -} -@infix func < (lhs:T,rhs:Double) -> Bool { - return (lhs.toDouble < rhs) -} -@infix func >= (lhs:T,rhs:Double) -> Bool { - return (lhs < rhs) == false -} -@infix func > (lhs:T,rhs:Double) -> Bool { - return (lhs <= rhs) == false -} - - -//SUBTRACTION -@infix func - (lhs: T, rhs:U) -> Double { - return lhs.toDouble - rhs.toDouble -} -@infix func - (lhs:Double, rhs:T) -> Double { - return lhs - rhs.toDouble -} -@infix func - (lhs:T, rhs:Double) -> Double { - return lhs.toDouble - rhs -} -@assignment @infix func -= (inout lhs:Double, rhs:T) { - lhs = lhs - rhs.toDouble -} - -//ADDITION -@infix func + (lhs: T, rhs:U) -> Double { - return lhs.toDouble + rhs.toDouble -} -@infix func + (lhs:Double, rhs:T) -> Double { - return lhs + rhs.toDouble -} -@infix func + (lhs:T, rhs:Double) -> Double { - return lhs.toDouble + rhs -} -@assignment @infix func += (inout lhs:Double, rhs:T) { - lhs = lhs + rhs.toDouble -} - -//MULTIPLICATION -@infix func * (lhs: T, rhs:U) -> Double { - return lhs.toDouble * rhs.toDouble -} -@infix func * (lhs:Double, rhs:T) -> Double { - return lhs * rhs.toDouble -} -@infix func * (lhs:T, rhs:Double) -> Double { - return lhs.toDouble * rhs -} -@assignment @infix func *= (inout lhs:Double, rhs:T) { - lhs = lhs * rhs.toDouble -} - -//DIVISION -@infix func / (lhs: T, rhs:U) -> Double { - return lhs.toDouble / rhs.toDouble -} -@infix func / (lhs: Double, rhs:T) -> Double { - return lhs / rhs.toDouble -} -@infix func / (lhs: T, rhs:Double) -> Double { - return lhs.toDouble / rhs -} -@assignment @infix func /= (inout lhs:Double, rhs:T) { - lhs = lhs / rhs.toDouble -} - #if !(arch(x86_64) || arch(arm64)) -extension CGPoint { - init(x:Double, y:Double) { - self.init(x:CGFloat(x), y:CGFloat(y)) +import CoreGraphics +extension Int { + @conversion func __conversion() -> CGFloat { + return CGFloat(self) } } - - -extension CGSize { - init(width:Double, height:Double) { - self.init(width:CGFloat(width), height:CGFloat(height)) +extension CGFloat { + @conversion func __conversion() -> Double { + return Double(self) } } -extension CGVector { - init(_ dx:Double, _ dy:Double) { - self.dx = CGFloat(dx) - self.dy = CGFloat(dy) +#endif +extension Int { + @conversion func __conversion() -> Double { + return Double(self) } } -#endif + diff --git a/TestsAndSample/TestsAndSample.xcodeproj/project.pbxproj b/TestsAndSample/TestsAndSample.xcodeproj/project.pbxproj index 08ccb86..608e1c1 100644 --- a/TestsAndSample/TestsAndSample.xcodeproj/project.pbxproj +++ b/TestsAndSample/TestsAndSample.xcodeproj/project.pbxproj @@ -15,8 +15,6 @@ 226C9AF819622393002E395C /* TestsIntArithmeticTesting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 226C9AF719622393002E395C /* TestsIntArithmeticTesting.swift */; }; 22AFC47F195F9D02003572F3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22AFC47E195F9D02003572F3 /* AppDelegate.swift */; }; 22AFC481195F9D02003572F3 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 22AFC480195F9D02003572F3 /* Images.xcassets */; }; - 22AFC48D195F9D02003572F3 /* TestsDoubleFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22AFC48C195F9D02003572F3 /* TestsDoubleFunctions.swift */; }; - 22AFC49D195FAF2A003572F3 /* TestsCGFloatFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22AFC49C195FAF2A003572F3 /* TestsCGFloatFunctions.swift */; }; 22AFC4A019603997003572F3 /* ScalarArithmetic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22AFC49F19603997003572F3 /* ScalarArithmetic.swift */; }; 22AFC4A119603997003572F3 /* ScalarArithmetic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22AFC49F19603997003572F3 /* ScalarArithmetic.swift */; }; 22AFC4A519603BBE003572F3 /* SuperTestsScalarArithmetic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22AFC4A419603BBE003572F3 /* SuperTestsScalarArithmetic.swift */; }; @@ -46,8 +44,6 @@ 22AFC480195F9D02003572F3 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 22AFC486195F9D02003572F3 /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 22AFC48B195F9D02003572F3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 22AFC48C195F9D02003572F3 /* TestsDoubleFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestsDoubleFunctions.swift; sourceTree = ""; }; - 22AFC49C195FAF2A003572F3 /* TestsCGFloatFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestsCGFloatFunctions.swift; sourceTree = ""; }; 22AFC49F19603997003572F3 /* ScalarArithmetic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScalarArithmetic.swift; sourceTree = ""; }; 22AFC4A419603BBE003572F3 /* SuperTestsScalarArithmetic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperTestsScalarArithmetic.swift; sourceTree = ""; }; 22AFC4A61960769A003572F3 /* SuperTestsScalarComparable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperTestsScalarComparable.swift; sourceTree = ""; }; @@ -116,8 +112,6 @@ 226C9AF319622257002E395C /* TestsDoubleArithmeticTesting.swift */, 226C9AF519622386002E395C /* TestsCGFloatArithmeticTesting.swift */, 226C9AF719622393002E395C /* TestsIntArithmeticTesting.swift */, - 22AFC48C195F9D02003572F3 /* TestsDoubleFunctions.swift */, - 22AFC49C195FAF2A003572F3 /* TestsCGFloatFunctions.swift */, 226C9AED19621EA9002E395C /* TestsDoubleComparable.swift */, 226C9AEF19621F3F002E395C /* TestsCGFloatComparable.swift */, 226C9AF119621F4B002E395C /* TestsIntComparable.swift */, @@ -250,7 +244,6 @@ buildActionMask = 2147483647; files = ( 226C9AF019621F3F002E395C /* TestsCGFloatComparable.swift in Sources */, - 22AFC49D195FAF2A003572F3 /* TestsCGFloatFunctions.swift in Sources */, 226C9AF419622257002E395C /* TestsDoubleArithmeticTesting.swift in Sources */, 22AFC4A519603BBE003572F3 /* SuperTestsScalarArithmetic.swift in Sources */, 226C9AEE19621EA9002E395C /* TestsDoubleComparable.swift in Sources */, @@ -259,7 +252,6 @@ 226C9AF619622386002E395C /* TestsCGFloatArithmeticTesting.swift in Sources */, 226C9AF819622393002E395C /* TestsIntArithmeticTesting.swift in Sources */, 226C9AF219621F4B002E395C /* TestsIntComparable.swift in Sources */, - 22AFC48D195F9D02003572F3 /* TestsDoubleFunctions.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/TestsAndSample/TestsAndSampleTests/TestsCGFloatFunctions.swift b/TestsAndSample/TestsAndSampleTests/TestsCGFloatFunctions.swift deleted file mode 100644 index ab09a05..0000000 --- a/TestsAndSample/TestsAndSampleTests/TestsCGFloatFunctions.swift +++ /dev/null @@ -1,76 +0,0 @@ -// -// TestsCGFloat.swift -// TestsAndSample -// -// Created by Seivan Heidari on 29/06/14. -// Copyright (c) 2014 Seivan Heidari. All rights reserved. -// - -import XCTest -import CoreGraphics - -class TestsCGFloat: XCTestCase { - let doubleValue:CGFloat = 5.5 - var expected:CGFloat? - let additionalValue = 7.7 - - -#if !(arch(x86_64) || arch(arm64)) - func testAcos() { - self.expected = acosf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.acos)) - } - func testAsin() { - self.expected = asinf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.asin) - } - func testAtan() { - self.expected = atanf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.atan)) - } - func testAtan2() { - self.expected = atan2f(self.doubleValue, CGFloat(self.additionalValue)) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.atan2(self.additionalValue))) - } - func testCos() { - self.expected = cosf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.cos)) - } - func testSin() { - self.expected = sinf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.sin)) - } - func testTan() { - self.expected = tanf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.tan)) - } - func testExp() { - self.expected = expf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.exp)) - } - func testExp2() { - self.expected = exp2f(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.exp2)) - } - func testLog() { - self.expected = logf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.log)) - } - func testLog10() { - self.expected = log10f(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.log10)) - } - func testLog2() { - self.expected = log2f(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.log2)) - } -// func testPow() { -// self.expected = powf(self.doubleValue, CGFloat(self.additionalValue)) -// XCTAssertEqualObjects(Double(self.expected!), (self.doubleValue.pow(self.additionalValue))) -// } - func testSqrt() { - self.expected = sqrtf(self.doubleValue) - XCTAssertEqualObjects(self.expected!, CGFloat(self.doubleValue.sqrt)) - } -#endif -} diff --git a/TestsAndSample/TestsAndSampleTests/TestsDoubleFunctions.swift b/TestsAndSample/TestsAndSampleTests/TestsDoubleFunctions.swift deleted file mode 100644 index d6ad2ef..0000000 --- a/TestsAndSample/TestsAndSampleTests/TestsDoubleFunctions.swift +++ /dev/null @@ -1,78 +0,0 @@ -// -// TestsAndSampleTests.swift -// TestsAndSampleTests -// -// Created by Seivan Heidari on 29/06/14. -// Copyright (c) 2014 Seivan Heidari. All rights reserved. -// - -import XCTest - - -class TestsDouble: XCTestCase { - let doubleValue = 5.5 - var expected:Double? - let additionalValue = 7.7 - - - - func testAcos() { - self.expected = acos(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.acos) - } - func testAsin() { - self.expected = asin(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.asin) - } - func testAtan() { - self.expected = atan(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.atan) - } - func testAtan2() { - self.expected = atan2(self.doubleValue, self.additionalValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.atan2(self.additionalValue)) - } - func testCos() { - self.expected = cos(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.cos) - } - func testSin() { - self.expected = sin(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.sin) - } - func testTan() { - self.expected = tan(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.tan) - } - func testExp() { - self.expected = exp(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.exp) - } - func testExp2() { - self.expected = exp2(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.exp2) - } - func testLog() { - self.expected = log(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.log) - } - func testLog10() { - self.expected = log10(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.log10) - } - func testLog2() { - self.expected = log2(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.log2) - } - func testPow() { - self.expected = pow(self.doubleValue, self.additionalValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.pow(self.additionalValue)) - } - func testSqrt() { - self.expected = sqrt(self.doubleValue) - XCTAssertEqualObjects(self.expected!, self.doubleValue.sqrt) - } - -} - - From e022f41436b85218f9807790164ac69b8f5f5d64 Mon Sep 17 00:00:00 2001 From: Seivan Heidari Date: Wed, 2 Jul 2014 15:07:49 +0200 Subject: [PATCH 2/3] Update readme. Remove all code. Allow for conversion instead. Also use Global math functions (Since they now do implicit casts) --- README.md | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b5b88ed..071bb79 100644 --- a/README.md +++ b/README.md @@ -8,40 +8,33 @@ let myHeight = 34.5 let myWidth = 100.23 let sizeOfWindow = CGSize(width:myWidth, height:myHeight) +let myDouble:Double = sizeOfWindow.height // .height is a CGFloat +let isLargerThan = 213.3 > sizeOfWindow.width // 213.3 is a Double ```` ### Overview -Adds math functions as properties and takes care of operators for interacting between different types of scalars. +Takes care of operators for interacting between different types of scalars. This library makes it easier to compare to ```Int```, ```Float``` and ```CGFloat``` regardless of architecture. +This also makes implicit casts to Double or CGFloat for arguments or variables that takes either types. + + ``var myDouble = 2.0`` will give you a ```Double``` and you'd want to use that with other types. Since ```CGFloat``` is not a ```Double``` on 32bit, it becomes hard to use CGGeomtry and frameworks like CoreGraphics or SpriteKit. This library makes it a little easier and hopefully Apple takes care of it soon. Works on both Mac OS and iOS. + + ### Math Functions -```swift -protocol ScalarFunctions { - var acos:Double {get} - var asin:Double {get} - var atan:Double {get} - func atan2(x:Double) -> Double - var cos:Double {get} - var sin:Double {get} - var tan:Double {get} - var exp:Double {get} - var exp2:Double {get} - var log:Double {get} - var log10:Double {get} - var log2:Double {get} - func pow(exponent:Double) -> Double - var sqrt:Double {get} -} +Many people disagreed with the global math functions being used as properties. I was on the fence on that one because I didn't want to write over them for 32 bit. However now that implicit casts are in place. This works on 32bit. +``` +let yay = abs(2.0) ``` From 6fa50f54970e42468ed9510b9e7c2de3d092fbde Mon Sep 17 00:00:00 2001 From: Seivan Heidari Date: Wed, 2 Jul 2014 15:17:17 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 071bb79..a279035 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Works on both Mac OS and iOS. ### Math Functions Many people disagreed with the global math functions being used as properties. I was on the fence on that one because I didn't want to write over them for 32 bit. However now that implicit casts are in place. This works on 32bit. -``` +```swift let yay = abs(2.0) ```