Overload +-*/ operator for Swift, make it easier to use (and not so strict)

Related tags

Math Easy-Cal-Swift
Overview

Easy-Cal-Swift

Overview

This file is an overloading of +-*/ operator for Swift, to make it easier to use (and not so strict)

It can make your life with Swift easier when doing calculation between different type. Swift has a type check and requires left operand and the right one the same type. It causes a lot of trouble that we must convert the type every time.

var a = 3
var b = 2.0
a + b  //Compile error
a - b  //Oops, error again
a * b  //Wtf..
a / b  //God please save me

//You have to write these instead
Double(a) + b
Double(a) - b
Double(a) * b
Double(a) / b

Yes, it is type safe, but it is also time wasted.

Usage

Just put the Easy-Cal.swift to your project, and you can now have a traditional C-like implicit conversion between Int, Float and Double. So you can write as you would expect without annoying error messages.

var a = 3
var b = 2.0
a + b  //5.0
a - b  //1.0
a * b  //6.0
a / b  //1.5

UInt is also implemented, but it seems there is a bug in this beta seed PlayGround, in which there is a fatal error when you assign an UInt value :(. Fortunately, the UInt works well in project.

Risk

Yes, you lose type safe when using these operators, which is opposite to Swift design. But I find at almost all the time, you will know what you are doing with these numbers and the conversion will be just exactly what you want.

You might also like...
A cross-platform Swift library for evaluating mathematical expressions at runtime

Introduction What? Why? How? Usage Installation Integration Symbols Variables Operators Functions Arrays Performance Caching Optimization Standard Lib

Numeric facilities for Swift

NumericAnnex NumericAnnex supplements the numeric facilities provided in the Swift standard library. Features The exponentiation operator ** and the c

Math expression parser built with Point•Free's swift-parsing package

swift-math-parser Basic math expression parser built with Point•Free's swift-parsing package. NOTE: currently, this uses a fork of that fixes a parsin

Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

MRFoundation - A library to complement the Swift Standard Library

MRFoundation MRFoundation is a library to complement the Swift Standard Library.

This app is a sample app that recognizes specific voice commands such as
This app is a sample app that recognizes specific voice commands such as "make it red", "make it blue", "make it green", and "make it black" and change the background color of the view in the frame.

VoiceOperationSample This app is a sample app that recognizes specific voice commands such as "make it red", "make it blue", "make it green", and "mak

Blobmorphism is a brand new design language I've created to break free of the material overload in iOS, built in SwiftUI. Everything feels smooth and fluid.
Blobmorphism is a brand new design language I've created to break free of the material overload in iOS, built in SwiftUI. Everything feels smooth and fluid.

Blobmorphism is a brand new design language I've created to break free of the material overload in iOS, built in SwiftUI. Everything feels smooth and fluid.

Decodable Simple and strict, yet powerful object mapping made possible by Swift 2's error handling.

Decodable Simple and strict, yet powerful object mapping made possible by Swift 2's error handling. Greatly inspired by Argo, but without a bizillion

Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.
Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.

CGLayout Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Has cross-hierarchy coordinate space. Implemen

This framework implements a strict JSON parser and generator in Objective-C.

SBJson 5 Chunk-based JSON parsing and generation in Objective-C. Overview SBJson's number one feature is stream/chunk-based operation. Feed the parser

Perl-like regex =~ operator for Swift

SwiftRegex Perl-like regex =~ operator for Swift This package implements a =~ string infix operator for use in testing regular expressions and retriev

A library that adds a throwing unwrap operator in Swift.

ThrowingUnwrap A simple package to add a throwing unwrap operator (~!) to Optionals in Swift. Import Add this to the package-wide dependencies in Pack

A replacement for as which runs in constant time instead of O(n) when the conformance is not satisfiedA replacement for as which runs in constant time instead of O(n) when the conformance is not satisfied

ZConform A replacement for as? which runs in constant time instead of O(n) when the conformance is not satisfied. How it works ZConform does a one-tim

Light-weight, operator-overloading-free complements to CoreGraphics!

Graphicz 🎨 Light-weight, operator-overloading-free complements to CoreGraphics! Even though I shipped it with my app, I still need to invest the time

Type strict builder class for NSAttributedString.
Type strict builder class for NSAttributedString.

StringStylizer Type strict builder class for NSAttributedString. What's this? StringStylizer makes NSAttributedString more intuitive with Method chain

EasierPath is a library to make UIBezierPath easier to use
EasierPath is a library to make UIBezierPath easier to use

EasierPath is a library to make UIBezierPath easier to use. More specifically, you can write more intuitive and concise code when you draw a straight line or a curve.

A guard to help you check if you make UI changes not in main thread
A guard to help you check if you make UI changes not in main thread

ODUIThreadGuard ODUIThreadGuard is a guard to help check if you make UI changes not in main thread. As Xcode 9 embedded this function into Xcode, ther

A small set of utilities to make working with CoreData and Swift a bit easier.

SuperRecord =================== SUPPORTS SWIFT 2.0 from Version = 1.4 ** SUPPORTS SWIFT 1.2 from Version = 1.3 Both iOS and WatchOS A Swift CoreData

NVDate is an extension of NSDate class (Swift4), created to make date and time manipulation easier.

NVDate is an extension of NSDate class (Swift4), created to make date and time manipulation easier. NVDate is testable and robust, we wrote intensive test to make sure everything is safe.

Comments
  • Add CGFloat support

    Add CGFloat support

    Thanks for your elegant work.

    There may comes times that we dealing with UI calculation especially when rotating views.

    So here CGFloat support comes.

    opened by victorteokw 1
Owner
Wei Wang
Developer, creator, proud father.
Wei Wang
Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)

Matft Matft is Numpy-like library in Swift. Function name and usage is similar to Numpy. Matft Feature & Usage Declaration MfArray MfType Subscription

null 80 Dec 21, 2022
SwiftMath is a Swift framework providing some useful math constructs and functions

SwiftMath is a Swift framework providing some useful math constructs and functions, like complex numbers, vectors, matrices, quaternions, and polynomials.

Matteo Battaglio 175 Dec 2, 2022
A set of protocols for Arithmetic, Statistics and Logical operations

Arithmosophi - Arithmosoϕ Arithmosophi is a set of missing protocols that simplify arithmetic and statistics on generic objects or functions. As Equat

Eric Marchand 66 Jul 9, 2022
Beautiful math equation rendering on iOS and MacOS

iosMath iosMath is a library for displaying beautifully rendered math equations in iOS and MacOS applications. It typesets formulae written using the

Kostub Deshmukh 1.3k Dec 21, 2022
Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives

Metron Geometry, simplified. Metron is a comprehensive collection of geometric functions and types that extend the 2D geometric primitives provided by

Toine Heuvelmans 1k Dec 5, 2022
Arbitrary-precision arithmetic in pure Swift

Overview API Documentation License Requirements and Integration Implementation Notes Full-width multiplication and division primitives Why is there no

null 707 Dec 19, 2022
A collection of functions for statistical calculation written in Swift.

σ (sigma) - statistics library written in Swift This library is a collection of functions that perform statistical calculations in Swift. It can be us

Evgenii Neumerzhitckii 658 Jan 5, 2023
Multi-dimensional Swift math

Upsurge Upsurge implements multi-dimensional data structures and operations. It brings numpy-like operations to Swift. Upsurge no longer supports DSP

Alejandro Isaza 180 Dec 20, 2022
Swift Matrix Library

Swift Matrix and Machine Learning Library Note: tensorflow/swift and apple/swift-numerics/issues/6 have or will have more complete support for NumPy-l

Scott Sievert 591 Sep 5, 2022
Swift Custom Operators for Mathematical Notation

Euler Euler uses custom operators in the "Math Symbols" character set to implement functions using traditional mathematical notation. Please keep in m

Mattt 1.1k Jan 4, 2023