Skip to content

Commit

Permalink
Merge pull request #4 from seivan/feature/use_conversion
Browse files Browse the repository at this point in the history
Feature/use conversion
  • Loading branch information
seivan committed Jul 3, 2014
2 parents 6ec38ec + 6fa50f5 commit 330232d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 384 deletions.
29 changes: 11 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 CGGeometry 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.
```swift
let yay = abs(2.0)
```


Expand Down
216 changes: 12 additions & 204 deletions ScalarArithmetic/ScalarArithmetic.swift
Original file line number Diff line number Diff line change
@@ -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 == <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
// return (lhs.toDouble == rhs.toDouble)
//}
//@infix func != <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
// return (lhs == rhs) == false
//}
@infix func <= <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs.toDouble <= rhs.toDouble)
}
@infix func < <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs.toDouble < rhs.toDouble)
}
@infix func >= <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs < rhs) == false
}
@infix func > <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs <= rhs) == false
}

//Equality Double<==>T
//@infix func == <T:ScalarArithmetic> (lhs:Double, rhs:T) -> Bool {
// return (lhs == rhs.toDouble)
//}
//@infix func != <T:ScalarArithmetic> (lhs:Double, rhs:T) -> Bool {
// return (lhs == rhs) == false
//}
@infix func <= <T:ScalarArithmetic> (lhs:Double, rhs:T) -> Bool {
return (lhs <= rhs.toDouble)
}
@infix func < <T:ScalarArithmetic> (lhs:Double, rhs:T) -> Bool {
return (lhs < rhs.toDouble)
}
@infix func >= <T:ScalarArithmetic> (lhs:Double, rhs:T) -> Bool {
return (lhs < rhs) == false
}
@infix func > <T:ScalarArithmetic> (lhs:Double, rhs:T) -> Bool {
return (lhs <= rhs) == false
}


//Equality T<==>Double
//@infix func == <T:ScalarArithmetic> (lhs:T,rhs:Double) -> Bool {
// return (lhs.toDouble == rhs)
//}
//@infix func != <T:ScalarArithmetic> (lhs:T,rhs:Double) -> Bool {
// return (lhs == rhs) == false
//}
@infix func <= <T:ScalarArithmetic> (lhs:T,rhs:Double) -> Bool {
return (lhs.toDouble <= rhs)
}
@infix func < <T:ScalarArithmetic> (lhs:T,rhs:Double) -> Bool {
return (lhs.toDouble < rhs)
}
@infix func >= <T:ScalarArithmetic> (lhs:T,rhs:Double) -> Bool {
return (lhs < rhs) == false
}
@infix func > <T:ScalarArithmetic> (lhs:T,rhs:Double) -> Bool {
return (lhs <= rhs) == false
}


//SUBTRACTION
@infix func - <T:ScalarArithmetic, U:ScalarArithmetic>(lhs: T, rhs:U) -> Double {
return lhs.toDouble - rhs.toDouble
}
@infix func - <T:ScalarArithmetic>(lhs:Double, rhs:T) -> Double {
return lhs - rhs.toDouble
}
@infix func - <T:ScalarArithmetic>(lhs:T, rhs:Double) -> Double {
return lhs.toDouble - rhs
}
@assignment @infix func -= <T:ScalarArithmetic>(inout lhs:Double, rhs:T) {
lhs = lhs - rhs.toDouble
}

//ADDITION
@infix func + <T:ScalarArithmetic, U:ScalarArithmetic>(lhs: T, rhs:U) -> Double {
return lhs.toDouble + rhs.toDouble
}
@infix func + <T:ScalarArithmetic>(lhs:Double, rhs:T) -> Double {
return lhs + rhs.toDouble
}
@infix func + <T:ScalarArithmetic>(lhs:T, rhs:Double) -> Double {
return lhs.toDouble + rhs
}
@assignment @infix func += <T:ScalarArithmetic>(inout lhs:Double, rhs:T) {
lhs = lhs + rhs.toDouble
}

//MULTIPLICATION
@infix func * <T:ScalarArithmetic, U:ScalarArithmetic>(lhs: T, rhs:U) -> Double {
return lhs.toDouble * rhs.toDouble
}
@infix func * <T:ScalarArithmetic>(lhs:Double, rhs:T) -> Double {
return lhs * rhs.toDouble
}
@infix func * <T:ScalarArithmetic>(lhs:T, rhs:Double) -> Double {
return lhs.toDouble * rhs
}
@assignment @infix func *= <T:ScalarArithmetic>(inout lhs:Double, rhs:T) {
lhs = lhs * rhs.toDouble
}

//DIVISION
@infix func / <T:ScalarArithmetic, U:ScalarArithmetic>(lhs: T, rhs:U) -> Double {
return lhs.toDouble / rhs.toDouble
}
@infix func / <T:ScalarArithmetic>(lhs: Double, rhs:T) -> Double {
return lhs / rhs.toDouble
}
@infix func / <T:ScalarArithmetic>(lhs: T, rhs:Double) -> Double {
return lhs.toDouble / rhs
}
@assignment @infix func /= <T:ScalarArithmetic>(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

8 changes: 0 additions & 8 deletions TestsAndSample/TestsAndSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -46,8 +44,6 @@
22AFC480195F9D02003572F3 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
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 = "<group>"; };
22AFC48C195F9D02003572F3 /* TestsDoubleFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestsDoubleFunctions.swift; sourceTree = "<group>"; };
22AFC49C195FAF2A003572F3 /* TestsCGFloatFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestsCGFloatFunctions.swift; sourceTree = "<group>"; };
22AFC49F19603997003572F3 /* ScalarArithmetic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScalarArithmetic.swift; sourceTree = "<group>"; };
22AFC4A419603BBE003572F3 /* SuperTestsScalarArithmetic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperTestsScalarArithmetic.swift; sourceTree = "<group>"; };
22AFC4A61960769A003572F3 /* SuperTestsScalarComparable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperTestsScalarComparable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand All @@ -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;
};
Expand Down
76 changes: 0 additions & 76 deletions TestsAndSample/TestsAndSampleTests/TestsCGFloatFunctions.swift

This file was deleted.

0 comments on commit 330232d

Please sign in to comment.