Commit Graph

67 Commits

Author SHA1 Message Date
Mateusz Pusz 16cdd23e04 style: pre-commit rules updated 2026-03-27 22:49:05 +00:00
Mateusz Pusz d8b72fc431 refactor: scaling the value by magnitude heavily refactored 2026-03-19 00:28:15 +01:00
Mateusz Pusz efbc844199 fix: fixed-point arithmetic for integer unit conversions (#580) (#764)
* Fix #580: use fixed-point arithmetic for integer unit conversions

Introduce a fixed-point implementation for unit conversions involving
integer representations, avoiding loss of significant digits that
previously occurred when the conversion factor was not a whole number.

New files:
- src/core/include/mp-units/bits/fixed_point.h: double_width_int<T> and
  fixed_point<T,n> types for exact rational scaling of integer values.
  Uses __int128 when available (__SIZEOF_INT128__) for 64-bit integers.
- src/core/include/mp-units/framework/scaling.h: public scaling_traits<>
  customization point and scale<To>(M, value) free function. Provides
  built-in specializations for floating-point and integer-like types.
- test/static/fixed_point_test.cpp: static assertions for the new types.
- test/runtime/fixed_point_test.cpp: runtime arithmetic edge-case tests.

Modified:
- sudo_cast.h: replace hand-rolled conversion_value_traits / sudo_cast_value
  machinery with a single scale<To::rep>(c_mag, ...) call.
- representation_concepts.h: add MagnitudeScalable concept; replace
  ComplexScalar with HasComplexOperations (which is its definition).
- customization_points.h: add unspecified_rep tag and declare the primary
  scaling_traits<> template.
- framework.h / CMakeLists.txt: wire in the new headers.
- hacks.h: add MP_UNITS_DIAGNOSTIC_IGNORE_PEDANTIC and
  MP_UNITS_DIAGNOSTIC_IGNORE_SIGN_CONVERSION macros.
- example/measurement.cpp: add scaling_traits specializations for
  measurement<T> to demonstrate the customization point.
- test/static/{international,usc}_test.cpp: disable two tests that are
  blocked on issue #614.

Co-authored-by: Tobias Hanhart <burnpanck@users.noreply.github.com>

* Fix value_Type typo in floating_point_scaling_factor_type specialization

The partial specialization for types with a nested value_type used
'value_Type' (capital T) instead of 'value_type', making the entire
specialization dead code as the requires-clause could never be satisfied.

Also fix 'mantiassa' -> 'mantissa' in the adjacent comment.

* Fix docstring typos in scaling_traits documentation

- 'quantitiy' -> 'quantity'
- 'dictatet' -> 'dictated'
- 'convetrible' -> 'convertible'
- 'implemenation' -> 'implementation'
- 'availabe' -> 'available'

* Fix conflict resolution error: keep ComplexScalar name from master

When resolving the merge conflict in representation_concepts.h, the
PR's renamed version of the concept ('HasComplexOperations') was used
instead of master's established name ('ComplexScalar'). The two concepts
are semantically equivalent — burnpanck simply renamed it in his branch.

Revert to the canonical 'ComplexScalar' name while retaining the new
'MagnitudeScalable' concept which was the actual addition from the PR.

* Fix measurement.cpp: remove duplicate class definition from merge

The PR branched from a version where measurement<T> was defined inline
in measurement.cpp. Master later moved the class to example/include/
measurement.h and changed measurement.cpp to #include that header.

The squash merge therefore introduced a duplicate definition: the class
from the header and the PR's inline class were both visible, causing
an 'ambiguous reference' error. Remove the now-redundant inline class;
the scaling_traits specializations added by the PR work correctly with
the class from measurement.h.

* style: pre-commit

* docs: chapters anchors improved in the "custom representation" chapter

* docs: value conversions chapter improved

* refactor: scaling support refactored

* fix: clang-16 crash fixed

* docs: `measurement` example documentation updated to match changes

* fix: use exact wide-integer arithmetic for rational unit conversions on all platforms

On ARM / Apple Silicon, long double == double (64-bit mantissa).  The old
fixed_point<T>(long double) initialiser lost ~12 bits of precision for 64-bit
integer types when representing the scaling ratio, producing an error of ~49
units for the 10/9 (degree → gradian) conversion with a 10^18 input value.

Fix by splitting the integer-path else-branch into two cases:

  • Pure rational M (is_integral(M * (denominator(M) / numerator(M))) == true):
    use (value * numerator) / denominator via double_width_int_for_t<> arithmetic.
    This is exact on every platform regardless of long double width.

  • Irrational M (involves π etc.): keep the long double fixed_point approximation.
    These conversions are inherently approximate; small values still produce correct
    truncated results on all platforms.

Update the test comment to reflect the new exact-arithmetic path.

Fixes CI failures on clang-18/ARM and apple-clang-16.

* fix: replace floating-point TeX-point test with exact integer equivalent

72.27 is not exactly representable as double (it rounds to 72.2699...96).
Multiplying by the conversion factor 100/7227 via long double gives a result
≥ 1.0 on x86 (80-bit long double, 64-bit mantissa) only by chance, but
0.99999...978 on ARM / Apple Silicon where long double == double (52-bit).

The correct mathematical statement is: 7227 tex_point = 100 inch (exact
rational relationship).  Use that integer form instead of the inexact 72.27
double literal so the test is correct and platform-independent.

---------

Co-authored-by: Tobias Hanhart <burnpanck@users.noreply.github.com>
2026-03-07 21:02:37 +01:00
Mateusz Pusz 6d10a154fa feat: 💥 pi and π is now a unit constant (pi magnitude constant renamed to pi_c) 2025-12-28 13:35:54 +01:00
Mateusz Pusz 4d8726fd87 feat(example): measurement.h extracted for easier reuse 2025-12-13 13:18:59 +01:00
Mateusz Pusz 35798a0f39 refactor: ostream.h header file made deprecated 2025-06-20 10:40:10 +02:00
Mateusz Pusz 47a82f466c refactor: scalar and complex renamed to real_scalar and complex_scalar respectively + concepts refactoring + electromagnetism fixes 2025-02-11 17:26:19 +01:00
Mateusz Pusz 465f88d500 refactor: 💥 is_XXX customization points for representation types removed 2024-11-26 14:48:08 +01:00
Mateusz Pusz 76057eaf7c feat(example): treat_as_floating_point specializations for examples' types removed 2024-10-26 19:15:34 +02:00
Jonas Hoppe 4f4da588b1 [msvc][fix] remove MP_UNITS_CONSTRAINED_AUTO_WORKAROUND in example 2024-09-10 10:15:24 +02:00
Jonas Hoppe 413966efca [msvc][fix] revert changes in example and test folder 2024-09-10 10:03:37 +02:00
Jonas Hoppe aebf55c09a Revert "Merge branch 'master-msvc-194-only-lib' into master-msvc-194"
This reverts commit e3034c5554, reversing
changes made to 8be458b0b7.
2024-09-09 12:10:39 +02:00
Jonas Hoppe 1d0dbf48b1 Merge remote-tracking branch 'mpusz/master' into master-msvc-194 2024-09-09 12:05:19 +02:00
Mateusz Pusz 2e840cfdb4 refactor: Repetitive inline constexpr removed as no longer needed
Not needed anymore as stated in cplusplus/draft#4601
2024-09-05 08:43:36 +02:00
Jonas Hoppe 93af5db137 [clang-format] 2024-09-04 10:30:39 +02:00
Jonas Hoppe 8be458b0b7 [msvc] remove MP_UNITS_CONSTRAINED_AUTO_WORKAROUND 2024-09-04 10:19:15 +02:00
Mateusz Pusz b870b85c25 feat: import std; support added
Resolves #595
2024-07-16 17:36:00 +02:00
Mateusz Pusz 75f719add7 refactor: code refactored to comply with clang-tidy 2024-05-08 11:12:38 +02:00
Mateusz Pusz f770057052 refactor: IWYU updates 2024-04-25 19:33:03 +02:00
Mateusz Pusz 8ea346be1c refactor: 💥 header files with the entire system definitions moved up in the directory tree 2024-04-25 16:34:11 +02:00
Mateusz Pusz c066104af7 refactor: 💥 framework.h introduced + IWYU 2024-04-24 21:09:29 +02:00
Mateusz Pusz 6914142275 fix: compat_macros included to fix the compilation after rebase 2024-01-06 09:25:17 +01:00
Johel Ernesto Guerrero Peña 7cb2099af5 feat: add module mp_units 2024-01-06 08:51:00 +01:00
Mateusz Pusz db4a24a197 refactor: MSVC workaround for constrained auto variable 2024-01-05 11:17:36 +01:00
Mateusz Pusz e23f038173 Merge branch 'master' into unit_compose_ext 2023-10-06 23:38:04 +02:00
Mateusz Pusz 25f986d32c fix: MSVC-related fixes and workarounds 2023-10-04 10:00:17 -06:00
Mateusz Pusz b2423bfded feat: quantities can now be multiplied and divided by units 2023-09-29 21:40:24 -06:00
Mateusz Pusz 42527d2bfb fix(example): measurement now properly propagates treat_as_floating_point 2023-09-10 14:17:34 +02:00
Mateusz Pusz ae92b49775 refactor: op[U] for quantity and quantity_point replaced with .in(U)
Resolves #469
2023-08-23 16:46:15 +02:00
Mateusz Pusz 52ce2b21f7 refactor: iostream.h renamed to ostream.h 2023-06-23 15:03:04 +02:00
Mateusz Pusz ba1f58af76 refactor: All the macros refactored to have MP_UNITS_ prefix 2023-06-21 18:05:21 +02:00
Mateusz Pusz 7d9ae3ec31 refactor: mp_units subdirectory renamed to mp-units 2023-05-25 12:47:10 +02:00
Mateusz Pusz 318b9e260b refactor(example): small refactoring of examples 2023-04-21 15:18:09 +01:00
Mateusz Pusz e98215c347 refactor: examples refactored to benefit from the latest features 2023-02-14 12:58:54 +01:00
Mateusz Pusz 859bc20fdc refactor: all examples refactored to use a simplified quantity creation 2023-02-08 21:47:48 -08:00
Mateusz Pusz 0cebb68e2e fix: broken concept names fixed 2023-02-03 16:58:33 +01:00
Mateusz Pusz 83aee04da0 refactor: quantity_io.h renamed to iostream.h 2023-02-03 16:57:48 +01:00
Mateusz Pusz 2cf736a1e6 feat: preventing possible future units crash when using namespace std;
Resolves #317
2022-12-29 20:18:48 +01:00
Mateusz Pusz 6b72dd39c1 refactor: headers with system definitions moved to _systems_ subdirectory 2022-12-29 19:27:54 +01:00
Mateusz Pusz 858cbb472f refactor: First examples refactored to a new quantity creation syntax 2022-12-22 18:06:20 +01:00
Mateusz Pusz 82b1f7ffc0 refactor(example): RepresentationOf support added to examples 2022-12-20 17:26:55 +01:00
Mateusz Pusz b4828d243d refactor: first examples refactored for V2 2022-11-11 10:33:24 -10:00
Mateusz Pusz b221dace3f style: clang-format applied to the remaining files 2022-04-02 21:36:42 +02:00
Mateusz Pusz 813197021f refactor: measurement example moved to a root examples directory 2021-04-15 14:05:36 +02:00
Mateusz Pusz b982921d27 refactor: References are now guarded UNITS_REFERENCES with (ON by default) + examples duplicated to subdirectories
Now References can be disabled to meassure a compile time impact. Also the same examples are now provided in two subdirectories to be able to easily compare the pros and cons of every quantity construction technique.
2021-04-06 15:57:28 +02:00
Mateusz Pusz 37cfc53d1d refactor: IWYU applied to the source files
`alternative_namespaces` examples left out as I consider them a subject for removal in the nearest future.
2021-03-30 13:21:05 +02:00
Mateusz Pusz 88e642ec74 refactor: QuantityValue concept renamed to Representation 2021-03-19 07:53:18 +01:00
Mateusz Pusz bbbb77286c refactor: physical replaced with isq
Refers to #249
2021-03-16 12:03:25 +01:00
Mateusz Pusz 23720d055e refactor: Refactored the library file tree
Resolves #249
2021-03-16 09:32:28 +01:00
Mateusz Pusz cff718301c refactor: quantity_io added 2020-12-28 15:18:36 +01:00