DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Verada.Vibe.Decimal_Digits

Correctly rounded decimal digits of a float, for Vibe's format specs.

Read the family guide for representation, selection and resource rules. The complete declaration page retains exact signatures/contracts; the source specification is the implementation baseline.

Availability and ownership

Convert gives the decimal digits of a Long_Float in Python's 'e', 'f' and '%' forms and repr's shortest form, correctly rounded as CPython's are: halves to even, on the exact binary value. Verada's body takes them from its run time (C's snprintf). Only the digits come from here; Verada.Vibe.Formatting lays them out. The compiler uses this unit when it translates Vibe; programs do not need to import it.

A client proof uses these contracts without proving the body again (see library assurance). Global => null excludes package-global effects, not changes made through an in out argument.

Import and instantiate

with Verada.Vibe.Decimal_Digits;
import Verada.Vibe.Decimal_Digits;

A Vibe module imports this unit's names with from Verada.Vibe.Decimal_Digits import Name; see library use across syntaxes. In Varc, keep each name's declared spelling and write & on arguments the call changes.

Contract and lifecycle reading

A Pre condition is the caller's obligation; a Post is what a normal return guarantees. What a postcondition does not state, a client cannot assume. Ghost functions are for contracts and ghost code. The family guide describes copying, lifetime, order and cost.

API 001

Mode (declaration) — Which digits Convert gives: Exponent (Python's 'e', Precision + 1 significant digits), Fixed ('f', the integer part's digits and then Precision more), Percent ('f' of the value times 100, rounded to a double as Python's is) and Shortest (repr's, the fewest significant digits that read back as the same value).

Declared interface:

type Mode is (Exponent, Fixed, Percent, Shortest);

API 002

Class (declaration) — Whether a value is finite, an infinity or a NaN.

Declared interface:

type Class is (Finite, Infinite, Not_A_Number);

API 003

Room (declaration) — How many digits Convert needs room for in a mode at a precision: Precision + 1 for Exponent, Precision + 401 for Fixed and Percent (a double's integer part has at most 309 digits), 17 for Shortest. Only memory limits the precision.

Declared interface:

function Room (How : Mode; Precision : Natural) return Long_Long_Long_Integer is
  (case How is
      when Exponent => Long_Long_Long_Integer (Precision) + 1,
      when Fixed | Percent => Long_Long_Long_Integer (Precision) + 401,
      when Shortest => 17);

API 004

Convert (declaration) — The decimal digits of X's magnitude, correctly rounded as CPython's are (halves to even, on the exact binary value), in Item (1 .. Count). Point is how many of them come before the decimal point; for Exponent and Shortest it can be 0 or less, or more than Count, and the decimal exponent is Point - 1. Kind is X's class (for Percent, that of X times 100) and Negative its sign bit, so -0.0 is negative. For a value that is not finite there are no digits, and Item (1) is '0'.

Before this operation, establish:

Item'First = 1 and then Long_Long_Long_Integer (Item'Length) = Room (How, Precision)

On normal return the declared contract establishes:

Count <= Item'Length
               and then (for all I in 1 .. Count => Item (I) in '0' .. '9')
               and then Point in -400 .. 400
               and then (case How is
                           when Exponent => Count = Precision + 1,
                           when Fixed | Percent => Count > Precision and then Point = Count - Precision,
                           when Shortest => Count <= 17)