Skip to content

Commit

Permalink
Proofreading of README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnickel committed May 26, 2022
1 parent a368b80 commit 45a0912
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Dimensional arithmetics in Swift

This package porovides an easy way of dealing with physical sizes. Performing complex arithmetics or unit conversion is as easy as dealing with ``Double`` values. Check out the code below, to get a glimps on what is posible using this package.
This package provides an easy and natural way of dealing with physical sizes. Performing complex arithmetics or unit conversion is as easy as using ``Double`` values. Check out the code below, to get a glimpse on what is possible with this package.

```swift
let area = 2.4[.m] * 5.86[.inch] // Area of a rectangle 2.4m x 5.86"
let force = 52.36[.lb] * SI.g // Force exceded by a mass of 52.36℔ under gravity
let preassure = force / area // Calculate the preassure.
print(preassure.convert(to: .Pa))
let force = 52.36[.lb] * SI.g // Force exceeded by a mass of 52.36℔ under gravity
let pressure = force / area // Calculate the pressure.
print(pressure.convert(to: .Pa))
```

> **TLDR**
> - Install this package like any other Swift package using the Swift package manager.
> - Construct an ``SI`` number using ``SI(_ value: Double, _ unit: SI.Unit)`` or the subscript notation
> ```swift
> let v1 = SI(2.5, .m / .s)
Expand All @@ -27,19 +28,19 @@ print(preassure.convert(to: .Pa))
## Basic usage
Every ``SI`` number consists of a ``value`` and a ``unit``. When performing arithmetic operations, the unit of a number is considered to ensure physical accuracy and proper conversion. Adding e.g. *1 m* to *500 mm* will return *1.5 m*, while an addition with *1 s* will result in an error since the physical dimentions of the units mismatch.
Every ``SI`` number consists of a ``value`` and a ``unit``. When performing arithmetic operations, the unit of a number is considered to ensure physical accuracy and proper conversion. Adding e.g. *1 m* to *500 mm* will return *1.5 m*, while an addition with *1 s* will result in an error since the physical dimensions of the units mismatch.
### Construction
To constract an ``SI``number you have three options:
To construct an ``SI``number you have three options:
1. Using the ``SI`` initializer
```swift
let myLength = SI(2.5, SI.Unit.m) // 2.5 meters
let myVelocity = SI(2.5, .m / .s) // 2.5 meters per second
let myScalar = SI(2.5) // 2.5 (scalar value)
```
2. Using the subscript shortcut. Whith this you can create an ``SI`` on the fly subscripting of a ``Double``
2. Using the subscript shortcut. With this you can create an ``SI`` on the fly subscripting of a ``Double``
```swift
let myLength = 2.5[SI.Unit.m] // 2.5 meters
let myVelocity = 2.5[.m / .s] // 2.5 meters per second
Expand All @@ -48,7 +49,7 @@ To constract an ``SI``number you have three options:

### Initialization

You can use ``SI`` like a ``Double`` without the struggle of keepng track of unit conversions. Here are some examples
You can use ``SI`` like a ``Double`` without the struggle of keeping track of unit conversions. Here are some examples

```swift
let l1 = 2[.m] // 2 meters
Expand All @@ -63,7 +64,7 @@ l1 ** 2 // 4m² (4[.m ** 2])
sqrt(l1 * l2) // 1m (1[.m])
```

> **Caution** Be careful when adding two ``SI`` numbers with mismathing physical dimensions e.g. ``1[.m] + 2[.s]`` since this will result in a precondition failure.
> **Caution** Be careful when adding two ``SI`` numbers with mismatching physical dimensions e.g. ``1[.m] + 2[.s]`` since this will result in a precondition failure.
### Conversion

Expand All @@ -81,7 +82,7 @@ If you find your unit missing in the library of ``SI.Unit``, you can easily crea

### Creating units on the fly

``SI.Unit`` supports multiplication, devision and exponentiation so creating a new unit in place is really easy.
``SI.Unit`` supports multiplication, division and exponentiation so creating a new unit in place is really easy.

```swift
let N = .kg * .m / .s ** 2 // Newton
Expand All @@ -90,7 +91,7 @@ let lb = 0.45359237 * .kg // Pound

### Prefixes

To add a prefix to a unit (eg. N → kN, m → μ), you can use the inbuilt methods.
To add a prefix to a unit (e.g. N → kN, m → μ), you can use the inbuilt methods.
```swift
let μm = μ(.m) // Micrometer: 1 μm = 10⁻⁶ m
let kN = k(.N) // Kilonewton: 1 kN = 10³ N
Expand All @@ -100,13 +101,13 @@ let kN = k(.N) // Kilonewton: 1 kN = 10³ N

To define a named ``SI.Unit`` you must use the initializer. You can create a new ``SI.Unit`` based of an existing one or one created on the fly. The name is used in the ``customDebugString``.

> **Tipp** Add your named units as an etension of ``SI.Unit`` to make them easily avialable.
> **Tip** Add your named units as an extension of ``SI.Unit`` to make them easily available.
```swift
extension SI.Unit {
public static let N = Self("N", kg * m / (s ** 2)) // Force in Newton
}
let Hz = SI.Unit("Hz", .scalar / .s) // Freqency in Hertz
let Hz = SI.Unit("Hz", .scalar / .s) // Frequency in Hertz
```

### Unit dimension
Expand All @@ -117,7 +118,7 @@ SI.Unit("m/s", .m / .s).dimension = [SI.Unit.Base.m: 1, SI.Unit.Base.s: -1]
```
The ``multiplicator`` is used to convert the unit to the SI system.

All standard base units are provided in the ``SI`` package. To define a new base unit (e.g. €, happieness,...) extend of ``SI.Unit.Base`` and use the default initializer and provide the standard symbol. Then define the standard unit of the new dimension providing the new ``dimension``, the ``multiplicator`` and a ``name``.
All standard base units are provided in the ``SI`` package. To define a new base unit (e.g. €, happiness,...) extend of ``SI.Unit.Base`` and use the default initializer and provide the standard symbol. Then define the standard unit of the new dimension providing the new ``dimension``, the ``multiplicator`` and a ``name``.
```swift
extension SI.Unit.Base{
public static let currency = Self(name: "")
Expand All @@ -132,4 +133,4 @@ extension SI.Unit {

You have found a bug 🐞 or have a question 🤔? Just send me a message or create an issue, and I will try to get back to you as soon as possible.

You want to devellop this package further? Just create a merge request or send me a message. Please add exaustive test to your new functionallity in order to keep this package as stable and reliable as possible. Also, please make sure to document your code to make it legible for other devs. If you have any questions just write me. Thank you very much for your contribution! ✌️
You want to develop this package further? Just create a merge request or send me a message. Please add exhaustive test to your new functionality in order to keep this package as stable and reliable as possible. Also, please make sure to document your code to make it legible for other devs. If you have any questions just write me. Thank you very much for your contribution! ✌️

0 comments on commit 45a0912

Please sign in to comment.