Isn’t Duration a fixed-point type?
If it is, then there isn’t a 'Pred attribute (IIRC), because it’s not a discrete type:
So then 0..Duration'Pred(1.0) wouldn’t work.
Foo : Duration := Duration’Pred (1.0); –- Compiles ok.
See Attributes RM K.2(181-184)
Frac returns Duration.
'Pred and 'Succ are defined for all scalar types [ARM K.2(181)]. For a fixed-point type T, T’Pred (X) is defined as X - T’Small.
For completeness, there is yet another way to truncate fixed-point values:
function Truncated (Value : Fix) return Fix is
Image : constant String := Value'Image;
Point : constant Natural := Ada.Strings.Fixed.Index (Image, ".");
begin -- Truncated
return Fix'Value (Image (Image'First .. Point) & '0');
end Truncated;
Indeed. For floating point types the whole process would be a lot easier as floating point types have 'Truncation — which fixed point don’t have.
Personally I think all fractional types should have a 'Integer (round towards 0) and 'Fraction attribute. Optionally 'Floor (next smaller integer) and Ceiling (next larger integer).
Converting into a string. Yes I think I did that once as well but I wonder about the efficiency here.
PS: For reasons unknown on Windows Long_Long_Integer is needed.
I seem to recall that the “default floating-point” of Windows/Intel is the 80-bit extended form; I may be misremembering/misconnecting, but if that’s the case then perhaps it’s because the conceptual Universal_Float/Universal_Fixed uses it “underneath” and thus requires the Long_Long_Integer to ‘fit’.
Honestly, I probably ought to review all the details of floating- and fixed-point, but there’s little appeal or motivation w/o some application-need.
What about Attributes of Fixed Point Types paragraph 12/6:
12/6 S’Truncation denotes a function with the following specification:
12.1/6 function S’Truncation (X : T)
return T
12.2/6 The function yields the value Ceiling(X) when X is negative, and Floor(X) otherwise.
Long_Long_Integer is never needed, and should never be used, since it may not exist.
Excellent. I was unaware that ISO/IEC 8652:2023 had added that. Too bad there are no compilers for it.
I am puzzled. The reason why there is no 'Truncation for fixed-point types is because it may be not implementable. The floating point model includes integral values, the fixed-point one (depending on the small) does not. Probably they meant a number closer to the integral value than small? Or maybe this one:
function S'Truncation (X : T) return universal_integer;
Some context.
You may remember mid-2025 WG9 approved Corrigendum 1 for Ada 2022, but ISO did not accept it and requested to submit it as Amendment 1 instead. The former primarily only allows fixes of the language definition, the latter allows also changes and additions to the language. A consequence is that some of the approved AIs could not be included in the Corrigendum, but can be and have been included in the Amendment. Among others: adding the Truncation and other attributes for fixed-point types.
Just a few days ago, I remembered that change while reviewing the Consolidated Ada 2022 RM that Ada-Europe is producing for publication in Springer’s LNCS series, as I was focusing on the differences between the master document for Ada RM + Corrigendum vs. Ada RM + Amendment.
Hope this clarifies…
A.5.4 (5/6) A fixed point type has integral values if its Small value is an integer or the reciprocal of an integer. A generic formal type is defined to have integral values.
(6/6) The following primitive function attributes are defined for every subtype S of a fixed point type T that has integral values.
(13/6) For all of these attributes, if T is a generic formal type, Program_Error is raised if the actual subtype does not have integral values.
Ok. Thanks for clarification.
Looks like GNAT hasn’t implemented it
:
src/hp41cx_tools-alarm.ads|136 col 15 error| prefix of "Truncation" attribute must be float type
src/hp41cx_tools-alarm.ads|136 col 15 error| prefix of "Truncation" attribute must be float type
src/adacl-calendar.ads|40 col 15 error| prefix of "Truncation" attribute must be float type
How could it - it’s just appeared in the newest (draft) RM 2022 AMD.