Back to OSS
Swift Package ユーティリティ

swift-physical-units

コンパイル時に次元検査を行う型安全な物理単位ライブラリ

Swift
unitstype-safephysics

English | 日本語

PhysicalUnits

Swift Platforms License

A Swift library for type-safe physical units. Unit consistency is verified at compile time with minimal runtime overhead.

Features

  • Type-safe unit conversion: Compile-time validation of conversions between different units
  • Physics formula operators: Natural syntax for formulas like F = m * a, E = F * d, P = V * I
  • SI prefix support: Automatic nano-to-tera coverage via the MetricUnit<BaseUnit> pattern
  • High performance: Optimized with @frozen and @inlinable
  • Multi-platform: iOS, macOS, watchOS, tvOS, visionOS

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/no-problem-dev/swift-physical-units.git", from: "1.0.0")
]

Usage

Basic Unit Conversion

import PhysicalUnits

// Length
let distance = Length(100, unit: .meters)
print(distance.kilometers)  // 0.1

// Mass
let mass = Mass(1, unit: .kilograms)
print(mass.grams)  // 1000.0

// Time
let time = Duration(1, unit: .hours)
print(time.seconds)  // 3600.0

Physics Formula Calculations

// Speed = Distance / Time
let distance = Length(100, unit: .kilometers)
let time = Duration(2, unit: .hours)
let speed: Speed = distance / time
print(speed.kilometersPerHour)  // 50.0

// Force = Mass × Acceleration
let mass = Mass(10, unit: .kilograms)
let acceleration = Acceleration(9.8, unit: .metersPerSecondSquared)
let force: Force = mass * acceleration
print(force.newtons)  // 98.0

// Power = Voltage × Current
let voltage = Voltage(100, unit: .volts)
let current = Current(5, unit: .amperes)
let power: Power = voltage * current
print(power.watts)  // 500.0

Ohm's Law

// V = IR
let current = Current(2, unit: .amperes)
let resistance = Resistance(50, unit: .ohms)
let voltage: Voltage = current * resistance
print(voltage.volts)  // 100.0

// I = V/R
let current2: Current = voltage / resistance
print(current2.amperes)  // 2.0

Rotational Motion

// Angular speed
let angle = Angle(360, unit: .degrees)
let time = Duration(1, unit: .seconds)
let angularSpeed: AngularSpeed = angle / time
print(angularSpeed.rpm)  // 60.0

// Linear speed = angular speed × radius
let radius = Length(0.5, unit: .meters)
let linearSpeed: Speed = angularSpeed * radius
print(linearSpeed.metersPerSecond)  // 3.14...

Battery Capacity

// Charge
let battery = Charge(milliampereHours: 5000)
print(battery.ampereHours)  // 5.0
print(battery.coulombs)  // 18000.0

// Discharge time
let current = Current(500, unit: .milliamperes)
let dischargeTime: Duration = battery / current
print(dischargeTime.hours)  // 10.0

Supported Dimensions

Base Dimensions

Dimension Type Base Unit
Length Length Meter (m)
Mass Mass Gram (g)
Time Duration Second (s)
Temperature Temperature Kelvin (K)
Electric current Current Ampere (A)
Angle Angle Radian (rad)

Derived Dimensions

Dimension Type Base Unit Formula
Speed Speed m/s v = d/t
Acceleration Acceleration m/s² a = v/t
Force Force N (kg·m/s²) F = ma
Energy Energy J (N·m) E = Fd
Power Power W (J/s) P = E/t
Voltage Voltage V V = IR
Resistance Resistance Ω R = V/I
Charge Charge C (A·s) Q = It
Angular speed AngularSpeed rad/s ω = θ/t
Frequency Frequency Hz f = 1/T
Area Area A = l × w
Volume Volume V = l × w × h
Pressure Pressure Pa P = F/A

Architecture

PhysicalUnits/
├── Core/
│   ├── Measurement.swift      # Generic measurement type
│   ├── Unit.swift             # Unit protocol
│   └── MetricUnit.swift       # SI prefix support
├── Dimensions/
│   ├── Length/                # Length
│   ├── Mass/                  # Mass
│   ├── Time/                  # Time
│   ├── Speed/                 # Speed
│   ├── Acceleration/          # Acceleration
│   ├── Force/                 # Force
│   ├── Energy/                # Energy
│   ├── Power/                 # Power
│   ├── Voltage/               # Voltage
│   ├── Current/               # Current
│   ├── Resistance/            # Resistance
│   ├── Charge/                # Charge
│   ├── Angle/                 # Angle
│   ├── AngularSpeed/          # Angular speed
│   ├── Frequency/             # Frequency
│   └── ...
└── Formulas/
    ├── KinematicsOperators.swift   # Kinematics
    ├── MechanicsOperators.swift    # Mechanics
    ├── ElectricityOperators.swift  # Electricity
    └── FrequencyOperators.swift    # Frequency & angular speed

License

MIT License — see LICENSE for details.

同じカテゴリの OSS — ユーティリティ

swift-env

Swift Package

環境変数アクセスを宣言的に扱う Swift マクロ

Swift
· ユーティリティ
swift-macroenvironmentconfiguration

swift-ios-recorder

Swift Package

iOS のオンデバイス・ランタイムレコーダー。画面・状態・ネットワーク・AI デバッグ情報をキャプチャし Mac/MCP/AI へストリームする

Swift
· ユーティリティ
iosdebuggingmcprecorder

© 2026 Kyoichi Taniguchi. All rights reserved.