View Source Trifle.Stats.Precision (Trifle.Stats v2.5.0)

Precision handling utilities using the Decimal library for high-accuracy arithmetic.

This module provides functions for converting between native numeric types and Decimal, performing high-precision calculations, and configuring precision levels throughout the Trifle.Stats library.

Configuration

Precision can be configured globally via application environment:

config :trifle_stats, precision: [
  enabled: true,
  scale: 10,
  rounding: :half_up
]

Usage Examples

# Convert to decimal
decimal = Trifle.Stats.Precision.to_decimal(42.5)

# Perform precise division
result = Trifle.Stats.Precision.divide(100, 3)

# Calculate percentage with precision
percentage = Trifle.Stats.Precision.percentage(sample, total)

# Convert result back to float if needed
float_result = Trifle.Stats.Precision.to_float(result)

Summary

Functions

Performs precise addition.

Calculates average of a list with precision.

Compares two numeric values, handling both Decimal and regular numbers. Returns -1, 0, or 1 for less than, equal to, or greater than.

Performs precise division with safe zero handling.

Checks if precision mode is enabled.

Finds maximum value in a list with precision handling.

Finds minimum value in a list with precision handling.

Performs precise multiplication.

Calculates percentage as (value / total) * 100 with precision.

Performs power operation with precision.

Gets the configured rounding mode.

Gets the configured decimal scale (number of decimal places).

Performs square root operation with precision.

Performs precise subtraction.

Calculates sum of a list with precision.

Converts a numeric value to Decimal with proper precision handling.

Converts a value back to float, handling both Decimal and numeric types.

Converts a value to the appropriate numeric type based on configuration. Returns Decimal if precision is enabled, otherwise returns float.

Functions

Performs precise addition.

Calculates average of a list with precision.

Compares two numeric values, handling both Decimal and regular numbers. Returns -1, 0, or 1 for less than, equal to, or greater than.

Performs precise division with safe zero handling.

Checks if precision mode is enabled.

Finds maximum value in a list with precision handling.

Finds minimum value in a list with precision handling.

Performs precise multiplication.

Link to this function

percentage(value, total)

View Source

Calculates percentage as (value / total) * 100 with precision.

Performs power operation with precision.

Gets the configured rounding mode.

Gets the configured decimal scale (number of decimal places).

Performs square root operation with precision.

Performs precise subtraction.

Calculates sum of a list with precision.

Converts a numeric value to Decimal with proper precision handling.

Converts a value back to float, handling both Decimal and numeric types.

Converts a value to the appropriate numeric type based on configuration. Returns Decimal if precision is enabled, otherwise returns float.