Feel.Analysis.Images.Number.Rounding (feel v0.1.0)

Copy Markdown View Source

The numbers floor, ceiling, round up, round down, round half up, round half down and decimal can give (Feel.Analysis.Images.Number, D-161).

A call rounds a number x to a scale s, a whole number of digits after the point (a fraction of the scale is cut off, and it has to be in -6111..6176, so valid_scales/0 is the open interval (-6112..6177)): the result is m * 10^-s for the whole number m that the mode chooses for x * 10^s. The seven modes are increasing step functions of x * 10^s with a step of one, so for a scale the numbers of an interval are the numbers m * 10^-s from the m of its lower end to the m of its upper end, every one of them (the step is one). The result is a number that can be written (the digits of m are those of x, give or take a carry that stays within 34 digits), so rounding is never null for a scale that is in range.

The m of an end is computed exactly, with integers (the run does the same, so the value is the run's). An end that is not in the interval is the limit of the steps next to it: floor of (0..1) is 0, and not 0..1, and the round half up of (0.5..1) is 1. The limit is the value at a number a quarter of a step on, except where the end is where the mode steps (breakpoint?/2).

A scale that is a set of whole numbers is taken one by one when it is a few, and a wider one gives the hull that holds every mode at every scale in it: x moved by at most a step of the coarsest scale (and by none of the sign: a mode keeps a number's sign). Both hold the numbers of the interval, as a hull (the numbers between the steps are in it). The exact integer step at an open endpoint can have more than 34 digits. Each returned endpoint is therefore cut outward to a representable FEEL number, down for the lower end and up for the upper end (D-220). Such a bound need not itself be a value the rounding function returns.

Summary

Types

How a mode chooses the whole number: x * 10^s rounded.

Functions

The numbers a mode can give for the numbers of the set, at the scales of the second set (which are in range).

The scales a call is in range for: those that are cut to a whole number in -6111..6176.

Types

mode()

@type mode() :: :floor | :ceiling | :up | :down | :half_up | :half_down | :half_even

How a mode chooses the whole number: x * 10^s rounded.

Functions

image(mode, numbers, scales)

The numbers a mode can give for the numbers of the set, at the scales of the second set (which are in range).

iex> alias Feel.Type.NumberSet
iex> alias Feel.Analysis.Images.Number.Rounding
iex> numbers = NumberSet.interval(Decimal.new("0.5"), Decimal.new("3.7"))
iex> Rounding.image(:floor, numbers, NumberSet.point(Decimal.new(0))) |> NumberSet.format()
"[0..3]"
iex> Rounding.image(:half_up, numbers, NumberSet.point(Decimal.new(0))) |> NumberSet.format()
"[1..4]"
iex> Rounding.image(:floor, NumberSet.interval({Decimal.new(0), :open}, {Decimal.new(1), :open}), NumberSet.point(Decimal.new(0))) |> NumberSet.format()
"0"

valid_scales()

@spec valid_scales() :: Feel.Type.NumberSet.t()

The scales a call is in range for: those that are cut to a whole number in -6111..6176.

iex> alias Feel.Type.NumberSet
iex> NumberSet.format(Feel.Analysis.Images.Number.Rounding.valid_scales())
"(-6112..6177)"