Prevodník Vlastných Jednotiek
Custom Units: Modeling, Formulas, and Best Practices
Define your own measurement units anchored to a 'Base Unit' or another custom unit. Model linear factors or full expressions, and organize consistent families for your project or domain.
Fundamental Concepts
Reference-Based Modeling
Your reference is another custom unit or 'Base Unit'.
The conversion expression maps input values into the reference unit's space (the system is intentionally unit-agnostic).
- Dimension SafetyBy selecting a reference, you implicitly tie the custom unit to that family. Keep families consistent (e.g., related units referencing the same base).
- ComposabilityChange the reference later without renaming the unit—only the expression needs adjustment.
- AuditabilityEvery unit has a single, clear definition: reference + expression.
Factor vs Expression
Simple units use a constant factor (e.g., 1 foo = 0.3048 × Base).
Advanced units can use expressions with functions (e.g., 10 * log(x / 1e-3)).
- Constant FactorsBest for fixed linear relationships (length scales, area ratios, etc.).
- ExpressionsUse math functions for derived or non-linear scales (ratios, logarithms, powers).
- ConstantsBuilt-in constants like PI, E, PHI, SQRT2, SQRT3, LN2, LN10, LOG2E, LOG10E, AVOGADRO, PLANCK, LIGHT_SPEED, GRAVITY, BOLTZMANN.
Naming, Symbols, and Consistency
Choose short, unambiguous symbols. Avoid collisions with existing standards.
Document the intent in your organization—what it measures and why it exists.
- ClarityPrefer concise symbols (1–4 chars recommended; UI allows up to 6).
- StabilityTreat symbols as stable identifiers across datasets and APIs.
- StyleUse SI-like casing where sensible (e.g., 'foo', 'kFoo', 'mFoo').
- A custom unit = reference unit + conversion expression.
- The reference anchors dimension; the expression defines value mapping.
- Prefer constant factors for linear scales; use expressions for special cases.
Formula Language
Expressions support numbers, the variable x (input value), alias value, constants (PI, E, PHI, SQRT2, SQRT3, LN2, LN10, LOG2E, LOG10E, AVOGADRO, PLANCK, LIGHT_SPEED, GRAVITY, BOLTZMANN), arithmetic operators, and common math functions. Expressions evaluate to a value in the chosen reference unit.
Operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | x + 2 |
| - | Subtraction/Unary Negation | x - 5, -x |
| * | Multiplication | 2 * x |
| / | Division | x / 3 |
| ** | Power (use **; ^ is auto-converted) | x ** 2 |
| () | Precedence | (x + 1) * 2 |
Functions
| Function | Signature | Example |
|---|---|---|
| sqrt | sqrt(x) | sqrt(x^2 + 1) |
| cbrt | cbrt(x) | cbrt(x) |
| pow | pow(a, b) | pow(0.3048, 2) |
| abs | abs(x) | abs(x) |
| min | min(a, b) | min(x, 100) |
| max | max(a, b) | max(x, 0) |
| round | round(x) | round(x * 1000) / 1000 |
| trunc | trunc(x) | trunc(x) |
| floor | floor(x) | floor(x) |
| ceil | ceil(x) | ceil(x) |
| sin | sin(x) | sin(PI/6) |
| cos | cos(x) | cos(PI/3) |
| tan | tan(x) | tan(PI/8) |
| asin | asin(x) | asin(0.5) |
| acos | acos(x) | acos(0.5) |
| atan | atan(x) | atan(1) |
| atan2 | atan2(y, x) | atan2(1, x) |
| sinh | sinh(x) | sinh(1) |
| cosh | cosh(x) | cosh(1) |
| tanh | tanh(x) | tanh(1) |
| ln | ln(x) | ln(x) |
| log | log(x) | log(100) |
| log2 | log2(x) | log2(8) |
| exp | exp(x) | exp(1) |
| degrees | degrees(x) | degrees(PI/2) |
| radians | radians(x) | radians(180) |
| percent | percent(value, total) | percent(25, 100) |
| factorial | factorial(n) | factorial(5) |
| gcd | gcd(a, b) | gcd(12, 8) |
| lcm | lcm(a, b) | lcm(12, 8) |
| clamp | clamp(value, min, max) | clamp(x, 0, 100) |
| sign | sign(x) | sign(-5) |
| nthRoot | nthRoot(value, n) | nthRoot(8, 3) |
Expression Rules
- x is the input value; alias value is also available.
- Use explicit multiplication (e.g., 2 * PI, not 2PI).
- Constants available: PI, E, PHI, SQRT2, SQRT3, LN2, LN10, LOG2E, LOG10E, AVOGADRO, PLANCK, LIGHT_SPEED, GRAVITY, BOLTZMANN.
- Angles for trigonometric functions are radians (use degrees() and radians() helper functions for conversion).
- Reference other custom units by name (snake_case) or symbol; their current toBase is injected as constants.
- Use ** for powers (the engine auto-converts ^ to **).
- Smart input normalization: ×, ÷, π, ², ³ are automatically converted to *, /, PI, ^2, ^3.
- Helper functions available: degrees(), radians(), percent(), factorial(), gcd(), lcm(), clamp(), sign(), nthRoot().
- Enhanced error detection prevents common mistakes (log of negative numbers, sqrt of negative numbers, division by zero).
- Custom unit referencing: Use other units as variables in expressions (e.g., 'x * A' where A is another custom unit).
- Whitespace is ignored; use parentheses to control precedence.
- Expressions must produce a finite numeric result for valid inputs.
- Use explicit multiplication (e.g., 2 * PI).
- Angles for trig functions are radians.
- log(x) is base 10; ln(x) is natural log (base e).
Dimensional Analysis & Strategies
This custom system is unit-agnostic. Model families by anchoring related units to the same 'Base Unit' (or a shared reference). Keep meaning consistent across the family you design.
Modeling Strategies
| Strategy | When to Use | Notes |
|---|---|---|
| Direct Factor | Linear relationships (e.g., 1 foo = k × Base). | Use a constant number (no x). Stable and precise. |
| Power Scaling | Derived from a base scale (k^2, k^3). | Use pow(k, n) where k is the base scale. |
| Ratio or Normalization | Units defined relative to a reference level (e.g., x / ref). | Useful for index-like measures; keep ref explicit in the expression. |
| Logarithmic Scale | Perceptual or power-ratio scales (e.g., dB-style 10 * log(x/ref)). | Ensure domain is positive; document reference value. |
| Affine Mapping | Rare cases with offsets (a * x + b). | Offsets change zero points—apply only when conceptually justified. |
Editor & Validation
Create units with a name, symbol (up to 6 chars), color tag, a reference (Base Unit or another custom unit), and a factor/expression. The editor validates formulas in real time with enhanced error detection and prevents circular references.
- Reference options include 'Base Unit' and existing custom units. Unsafe options that would create cycles are filtered out automatically.
- Variables: use x (or value) for the input value. Reference other custom units by snake_case name or by symbol; their current toBase values are injected as constants.
- Supported constants: PI, E, PHI, SQRT2, SQRT3, LN2, LN10, LOG2E, LOG10E, AVOGADRO, PLANCK, LIGHT_SPEED, GRAVITY, BOLTZMANN.
- Core functions: sqrt, cbrt, pow, abs, min, max, round, trunc, floor, ceil, sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, ln, log, log2, exp.
- Helper functions: degrees(), radians(), percent(), factorial(), gcd(), lcm(), clamp(), sign(), nthRoot() for enhanced UX.
- Operators: +, -, *, /, ** for power. Smart input normalization: ×, ÷, π, ², ³ are automatically converted.
- Real-time validation with preview (e.g., 10 x → result), complexity classification (simple/moderate/complex), and context-aware suggestions.
- Enhanced error detection catches common mistakes: logarithms of non-positive numbers, square roots of negative numbers, division by zero.
- Advanced cycle detection prevents units from depending on themselves (directly or indirectly) with clear error messages.
- Interactive help panel with categorized examples, clickable formula snippets, and custom unit buttons for easy insertion.
Best Practices
- Prefer a constant factor if possible; expressions only when necessary.
- Choose a reference unit that is stable, widely understood, and unlikely to change.
- Avoid circular chains of references; keep graphs acyclic.
- Add sample values and cross-check with independent calculators or known identities.
- Keep symbols short, unique, and documented for your organization.
- If using logs, record the reference value, base, and intended domain of x.
- Test with 3–5 representative values and verify round-trip conversions.
- Avoid circular references; pick a stable reference unit.
- Document assumptions (domains, offsets, typical ranges).
Starter Templates & Examples
These examples illustrate common modeling patterns in this custom-only system. Replace constants and references with your needs.
| Name | Formula | Reference | Notes |
|---|---|---|---|
| Base-Scaled Unit (foo) | 0.3048 | Base Unit | Defines 1 foo = 0.3048 × Base (simple linear factor). |
| Power-Scaled (foo²) | pow(0.3048, 2) | Base Unit | Derived from a base scale (k^2). |
| Volume-Scaled (foo³) | pow(0.3048, 3) | Base Unit | Derived from a base scale (k^3). |
| Index from Reference | x / 42 | Base Unit | Normalize by a fixed level (domain x > 0). |
| Power Ratio (dB-style) | 10 * log(x / 0.001) | Base Unit | Logarithmic measure relative to 1 mW (example). Ensure x > 0. |
| Geometric Factor | 2 * PI * 0.5 | Base Unit | Example of constants and multiplication. |
| Reference Other Custom Unit | A * 2 | Custom Unit A | Use another unit's symbol/name as a constant in expressions. |
| Complex Unit Relationship | sqrt(x^2 + base_length^2) | Base Unit | Pythagorean relationship using custom unit 'base_length' as a constant. |
| Scaled Unit with Offset | x * scale_factor + offset_unit | Base Unit | Linear transformation using two other custom units as constants. |
| Percentage of Reference Unit | percent(x, reference_value) | Base Unit | Express input as percentage of another custom unit using helper function. |
| Clamped Unit Range | clamp(x * multiplier, min_unit, max_unit) | Base Unit | Constrain values between two custom unit constants using clamp helper. |
| Unit Ratio with GCD | x / gcd(x, common_divisor) | Base Unit | Mathematical relationship using GCD helper with custom unit constant. |
| Angular Conversion Chain | degrees(x * PI / reference_angle) | Custom Angular Unit | Convert to degrees using custom angle unit and degrees() helper function. |
Governance & Collaboration
- Maintain a catalog of approved custom units with owners and review dates.
- Use versioning when definitions evolve; avoid breaking changes to symbols.
- Record provenance for constants and references (standards, literature, internal docs).
- Automate validation tests (range checks, sample conversions, monotonicity).
FAQ
Should I use a constant factor or an expression?
Prefer a constant factor whenever the relationship is linear and fixed. Use expressions only when the mapping depends on x or requires functions (powers, logs, trig).
How do I pick a reference unit?
Choose a stable, widely understood unit that captures the dimension you intend (e.g., meter for length, m² for area). The reference anchors dimensional meaning.
Are angles in degrees or radians?
Radians. Convert degrees by multiplying by PI/180 before using trig functions.
Can I chain custom units?
Yes, but avoid cycles. Keep the graph acyclic and document the chain to preserve clarity.
Kompletný Adresár Nástrojov
Všetkých 71 nástrojov dostupných na UNITS