HAIDOX
HAIDOX Help

Math Engine Reference: Finance Fixed

개요

FINANCE_FIXED용 통화, rate, FX, 배분, 이자와 감사 C ABI.

모든 수치는 decimal fixed 또는 정수다. Binary32/64 입력과 암묵 변환 API는 없으며 통화 minor-unit과 반올림 정책은 호출자가 명시한다. 통화 code는 로캘이나 외부 통화 database를 조회하지 않는 3자리 대문자 ASCII 식별자다. 실패하면 output을 보존한다.

공개 API

Header 사용 시작

이 module의 public declaration을 사용하려면 installed SDK의 다음 header를 include한다.

#include <math_engine/math_engine_finance_fixed.h>

Type과 callback

math_engine_finance_rate_kind_t

Rate의 의미를 구분하는 식별자다.

typedef enum math_engine_finance_rate_kind {
    MATH_ENGINE_FINANCE_RATE_INVALID = 0, /**< 초기화되지 않았거나 알려지지 않은 rate-kind sentinel이다. */
    MATH_ENGINE_FINANCE_RATE_GENERIC = 1, /**< 별도 percentage/period 의미를 강제하지 않는 무차원 multiplier rate다. */
    MATH_ENGINE_FINANCE_RATE_PERCENTAGE = 2, /**< Fee 또는 tax에 적용하는 무차원 percentage rate다. 7.5%는 0.075다. */
    MATH_ENGINE_FINANCE_RATE_PER_PERIOD = 3 /**< 단리·복리의 한 기간마다 적용하는 무차원 rate다. */
} math_engine_finance_rate_kind_t;
  • math_engine_finance_rate_kind_t.MATH_ENGINE_FINANCE_RATE_INVALID

    초기화되지 않았거나 알려지지 않은 rate-kind sentinel이다.

    MATH_ENGINE_FINANCE_RATE_INVALID = 0,
    
  • math_engine_finance_rate_kind_t.MATH_ENGINE_FINANCE_RATE_GENERIC

    별도 percentage/period 의미를 강제하지 않는 무차원 multiplier rate다.

    MATH_ENGINE_FINANCE_RATE_GENERIC = 1,
    
  • math_engine_finance_rate_kind_t.MATH_ENGINE_FINANCE_RATE_PERCENTAGE

    Fee 또는 tax에 적용하는 무차원 percentage rate다. 7.5%는 0.075다.

    MATH_ENGINE_FINANCE_RATE_PERCENTAGE = 2,
    
  • math_engine_finance_rate_kind_t.MATH_ENGINE_FINANCE_RATE_PER_PERIOD

    단리·복리의 한 기간마다 적용하는 무차원 rate다.

    MATH_ENGINE_FINANCE_RATE_PER_PERIOD = 3,
    

math_engine_finance_fx_direction_t

FX quote의 rate를 곱할지 나눌지 결정하는 방향이다.

typedef enum math_engine_finance_fx_direction {
    MATH_ENGINE_FINANCE_FX_INVALID = 0, /**< 초기화되지 않았거나 알려지지 않은 FX direction sentinel이다. */
    /** quote.rate는 target currency / source currency다. */
    MATH_ENGINE_FINANCE_FX_TARGET_PER_SOURCE = 1,
    /** quote.rate는 source currency / target currency다. */
    MATH_ENGINE_FINANCE_FX_SOURCE_PER_TARGET = 2
} math_engine_finance_fx_direction_t;
  • math_engine_finance_fx_direction_t.MATH_ENGINE_FINANCE_FX_INVALID

    초기화되지 않았거나 알려지지 않은 FX direction sentinel이다.

    MATH_ENGINE_FINANCE_FX_INVALID = 0,
    
  • math_engine_finance_fx_direction_t.MATH_ENGINE_FINANCE_FX_TARGET_PER_SOURCE

    quote.rate는 target currency / source currency다.

    /** quote.rate는 target currency / source currency다. */
        MATH_ENGINE_FINANCE_FX_TARGET_PER_SOURCE = 1,
    
  • math_engine_finance_fx_direction_t.MATH_ENGINE_FINANCE_FX_SOURCE_PER_TARGET

    quote.rate는 source currency / target currency다.

    /** quote.rate는 source currency / target currency다. */
        MATH_ENGINE_FINANCE_FX_SOURCE_PER_TARGET = 2,
    

math_engine_finance_operation_t

감사 record에 기록되는 금융 연산 식별자다.

typedef enum math_engine_finance_operation {
    MATH_ENGINE_FINANCE_OPERATION_INVALID = 0, /**< Audit record가 초기화 상태이거나 provenance가 없음을 나타낸다. */
    MATH_ENGINE_FINANCE_OPERATION_QUANTIZE = 1, /**< Money amount를 currency minor-unit scale로 한 번 양자화했다. */
    MATH_ENGINE_FINANCE_OPERATION_FEE = 2, /**< 한 money value에 percentage fee rate를 적용했다. */
    MATH_ENGINE_FINANCE_OPERATION_TAX = 3, /**< 한 money value에 percentage tax rate를 적용했다. */
    MATH_ENGINE_FINANCE_OPERATION_SIMPLE_INTEREST = 4, /**< Principal에 기간 수를 곱한 단리 interest를 계산했다. */
    MATH_ENGINE_FINANCE_OPERATION_COMPOUND_INTEREST = 5, /**< 각 기간에 booking/rounding하는 복리 balance를 계산했다. */
    MATH_ENGINE_FINANCE_OPERATION_FX_MULTIPLY = 6, /**< TARGET_PER_SOURCE quote rate를 source amount에 곱했다. */
    MATH_ENGINE_FINANCE_OPERATION_FX_DIVIDE = 7, /**< SOURCE_PER_TARGET quote rate로 source amount를 나눴다. */
    MATH_ENGINE_FINANCE_OPERATION_AGGREGATE = 8, /**< 같은 통화 money 배열을 순서 독립적으로 합산했다. */
    MATH_ENGINE_FINANCE_OPERATION_PROPORTIONAL_ALLOCATION = 9, /**< Booked minor units를 largest-remainder 방식으로 보존 배분했다. */
    MATH_ENGINE_FINANCE_OPERATION_BATCH_FEE = 10, /**< Money 배열에 fee rate를 deterministic block 순서로 적용했다. */
    MATH_ENGINE_FINANCE_OPERATION_BATCH_TAX = 11 /**< Money 배열에 tax rate를 deterministic block 순서로 적용했다. */
} math_engine_finance_operation_t;
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_INVALID

    Audit record가 초기화 상태이거나 provenance가 없음을 나타낸다.

    MATH_ENGINE_FINANCE_OPERATION_INVALID = 0,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_QUANTIZE

    Money amount를 currency minor-unit scale로 한 번 양자화했다.

    MATH_ENGINE_FINANCE_OPERATION_QUANTIZE = 1,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_FEE

    한 money value에 percentage fee rate를 적용했다.

    MATH_ENGINE_FINANCE_OPERATION_FEE = 2,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_TAX

    한 money value에 percentage tax rate를 적용했다.

    MATH_ENGINE_FINANCE_OPERATION_TAX = 3,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_SIMPLE_INTEREST

    Principal에 기간 수를 곱한 단리 interest를 계산했다.

    MATH_ENGINE_FINANCE_OPERATION_SIMPLE_INTEREST = 4,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_COMPOUND_INTEREST

    각 기간에 booking/rounding하는 복리 balance를 계산했다.

    MATH_ENGINE_FINANCE_OPERATION_COMPOUND_INTEREST = 5,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_FX_MULTIPLY

    TARGET_PER_SOURCE quote rate를 source amount에 곱했다.

    MATH_ENGINE_FINANCE_OPERATION_FX_MULTIPLY = 6,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_FX_DIVIDE

    SOURCE_PER_TARGET quote rate로 source amount를 나눴다.

    MATH_ENGINE_FINANCE_OPERATION_FX_DIVIDE = 7,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_AGGREGATE

    같은 통화 money 배열을 순서 독립적으로 합산했다.

    MATH_ENGINE_FINANCE_OPERATION_AGGREGATE = 8,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_PROPORTIONAL_ALLOCATION

    Booked minor units를 largest-remainder 방식으로 보존 배분했다.

    MATH_ENGINE_FINANCE_OPERATION_PROPORTIONAL_ALLOCATION = 9,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_BATCH_FEE

    Money 배열에 fee rate를 deterministic block 순서로 적용했다.

    MATH_ENGINE_FINANCE_OPERATION_BATCH_FEE = 10,
    
  • math_engine_finance_operation_t.MATH_ENGINE_FINANCE_OPERATION_BATCH_TAX

    Money 배열에 tax rate를 deterministic block 순서로 적용했다.

    MATH_ENGINE_FINANCE_OPERATION_BATCH_TAX = 11,
    

math_engine_finance_currency_t

ISO 스타일 3자리 대문자 ASCII code와 0..18 minor-unit scale.

typedef struct math_engine_finance_currency {
    char code[4]; /**< 세 자리 uppercase ASCII currency code와 네 번째 NUL byte다. 외부 catalog를 참조하지 않는다. */
    uint32_t minor_unit; /**< Booked amount의 decimal scale이며 inclusive 0..18 범위다. */
} math_engine_finance_currency_t;
  • math_engine_finance_currency_t.code

    세 자리 uppercase ASCII currency code와 네 번째 NUL byte다. 외부 catalog를 참조하지 않는다.

    char code[4];
    
  • math_engine_finance_currency_t.minor_unit

    Booked amount의 decimal scale이며 inclusive 0..18 범위다.

    uint32_t minor_unit;
    

math_engine_finance_money_t

통화 식별자와 decimal 금액. 성공한 operation은 amount.scale을 minor_unit으로 만든다.

typedef struct math_engine_finance_money {
    math_engine_decimal_fixed_t amount; /**< 통화 단위의 decimal 금액이다. 성공한 booking 결과 scale은 =currency.minor_unit=이다. */
    math_engine_finance_currency_t currency; /**< Amount의 통화 code와 minor-unit quantum이다. */
    uint64_t reserved; /**< 향후 ABI 확장용이다. Caller와 producer 모두 0을 기록해야 한다. */
} math_engine_finance_money_t;
  • math_engine_finance_money_t.amount

    통화 단위의 decimal 금액이다. 성공한 booking 결과 scale은 currency.minor_unit 이다.

    math_engine_decimal_fixed_t amount;
    
  • math_engine_finance_money_t.currency

    Amount의 통화 code와 minor-unit quantum이다.

    math_engine_finance_currency_t currency;
    
  • math_engine_finance_money_t.reserved

    향후 ABI 확장용이다. Caller와 producer 모두 0을 기록해야 한다.

    uint64_t reserved;
    

math_engine_finance_rate_t

차원 없는 decimal rate. 7.5%는 value=0.075로 표현한다.

typedef struct math_engine_finance_rate {
    math_engine_decimal_fixed_t value; /**< 무차원 decimal rate다. Percentage 7.5%는 정확히 0.075로 표현한다. */
    uint32_t kind; /**< Rate의 소비 계약을 정하는 =math_engine_finance_rate_kind_t= 값이다. */
    uint32_t reserved; /**< 향후 ABI 확장용이다. Caller와 producer 모두 0을 기록해야 한다. */
} math_engine_finance_rate_t;
  • math_engine_finance_rate_t.value

    무차원 decimal rate다. Percentage 7.5%는 정확히 0.075로 표현한다.

    math_engine_decimal_fixed_t value;
    
  • math_engine_finance_rate_t.kind

    Rate의 소비 계약을 정하는 math_engine_finance_rate_kind_t 값이다.

    uint32_t kind;
    
  • math_engine_finance_rate_t.reserved

    향후 ABI 확장용이다. Caller와 producer 모두 0을 기록해야 한다.

    uint32_t reserved;
    

math_engine_finance_fx_quote_t

두 통화와 명시적 방향을 포함하는 decimal FX quote.

typedef struct math_engine_finance_fx_quote {
    math_engine_finance_currency_t source_currency; /**< Conversion input amount가 사용해야 하는 통화다. */
    math_engine_finance_currency_t target_currency; /**< Conversion output amount가 사용하는 서로 다른 통화다. */
    math_engine_decimal_fixed_t rate; /**< Source/target 관계를 =direction=에 따라 해석하는 strictly positive decimal quote rate다. */
    uint32_t direction; /**< Rate를 곱할지 나눌지 정하는 =math_engine_finance_fx_direction_t= 값이다. */
    uint32_t reserved; /**< 향후 ABI 확장용이다. Caller와 producer 모두 0을 기록해야 한다. */
} math_engine_finance_fx_quote_t;
  • math_engine_finance_fx_quote_t.source_currency

    Conversion input amount가 사용해야 하는 통화다.

    math_engine_finance_currency_t source_currency;
    
  • math_engine_finance_fx_quote_t.target_currency

    Conversion output amount가 사용하는 서로 다른 통화다.

    math_engine_finance_currency_t target_currency;
    
  • math_engine_finance_fx_quote_t.rate

    Source/target 관계를 direction 에 따라 해석하는 strictly positive decimal quote rate다.

    math_engine_decimal_fixed_t rate;
    
  • math_engine_finance_fx_quote_t.direction

    Rate를 곱할지 나눌지 정하는 math_engine_finance_fx_direction_t 값이다.

    uint32_t direction;
    
  • math_engine_finance_fx_quote_t.reserved

    향후 ABI 확장용이다. Caller와 producer 모두 0을 기록해야 한다.

    uint32_t reserved;
    

math_engine_finance_policy_t

금융 operation의 명시적 rounding, 기간, thread와 memory 한계.

typedef struct math_engine_finance_policy {
    uint32_t struct_size; /**< Caller가 =sizeof(math_engine_finance_policy_t)=로 설정하는 byte 크기다. */
    uint32_t struct_version; /**< =MATH_ENGINE_FINANCE_POLICY_VERSION_1=인 policy ABI version이다. */
    uint32_t rounding; /**< 모든 minor-unit 양자화에 적용하는 =math_engine_decimal_rounding_t= 값이다. */
    uint32_t requested_thread_count; /**< Batch worker 수다. 0이면 hardware와 ceiling 안에서 자동 선택한다. */
    uint64_t maximum_periods; /**< Interest operation이 허용하는 inclusive period-count ceiling이다. */
    uint64_t memory_budget_bytes; /**< Caller workspace와 내부 임시 bookkeeping에 허용한 byte ceiling이다. */
    uint32_t flags; /**< Version 1 finance-policy flag bitset이다. 정의되지 않은 bit는 0이어야 한다. */
    uint32_t reserved; /**< 향후 ABI 확장용이다. Caller는 0으로 설정해야 하며 nonzero policy는 INVALID_ARGUMENT로 거부된다. */
} math_engine_finance_policy_t;
  • math_engine_finance_policy_t.struct_size

    Caller가 sizeof(math_engine_finance_policy_t) 로 설정하는 byte 크기다.

    uint32_t struct_size;
    
  • math_engine_finance_policy_t.struct_version

    MATH_ENGINE_FINANCE_POLICY_VERSION_1 인 policy ABI version이다.

    uint32_t struct_version;
    
  • math_engine_finance_policy_t.rounding

    모든 minor-unit 양자화에 적용하는 math_engine_decimal_rounding_t 값이다.

    uint32_t rounding;
    
  • math_engine_finance_policy_t.requested_thread_count

    Batch worker 수다. 0이면 hardware와 ceiling 안에서 자동 선택한다.

    uint32_t requested_thread_count;
    
  • math_engine_finance_policy_t.maximum_periods

    Interest operation이 허용하는 inclusive period-count ceiling이다.

    uint64_t maximum_periods;
    
  • math_engine_finance_policy_t.memory_budget_bytes

    Caller workspace와 내부 임시 bookkeeping에 허용한 byte ceiling이다.

    uint64_t memory_budget_bytes;
    
  • math_engine_finance_policy_t.flags

    Version 1 finance-policy flag bitset이다. 정의되지 않은 bit는 0이어야 한다.

    uint32_t flags;
    
  • math_engine_finance_policy_t.reserved

    향후 ABI 확장용이다. Caller는 0으로 설정해야 하며 nonzero policy는 INVALID_ARGUMENT로 거부된다.

    uint32_t reserved;
    

math_engine_finance_audit_t

Booked 값과 scale/rounding/remainder/backend provenance의 결정적 감사 record.

typedef struct math_engine_finance_audit {
    uint32_t struct_size; /**< Caller가 =sizeof(math_engine_finance_audit_t)=로 설정하는 byte 크기다. */
    uint32_t struct_version; /**< =MATH_ENGINE_FINANCE_AUDIT_VERSION_1=인 audit ABI version이다. */
    math_engine_finance_money_t value; /**< Operation이 최종 booking한 통화 minor-unit 금액이다. */
    uint32_t operation; /**< Booked value를 만든 =math_engine_finance_operation_t= 값이다. */
    uint32_t rounding; /**< Booking에 적용한 =math_engine_decimal_rounding_t= 값이다. */
    int32_t input_scale; /**< 주 입력 decimal amount의 원래 scale이다. */
    int32_t output_scale; /**< =value.amount.scale=과 같은 booked minor-unit scale이다. */
    uint32_t flags; /**< =MATH_ENGINE_FINANCE_AUDIT_FLAG_*= provenance bit의 조합이다. */
    uint32_t selected_thread_count; /**< 실제 batch 실행에 선택한 worker 수다. Scalar operation은 1이다. */
    uint64_t operand_count; /**< Aggregate/batch/allocation에 소비한 money 또는 weight 원소 수다. */
    uint64_t period_count; /**< Interest calculation에 적용한 기간 수다. 다른 operation은 0이다. */
    int64_t remainder_units; /**< Allocation/rounding에서 결정적으로 배분하거나 버린 signed minor-unit 수다. */
    uint64_t decimal_operation_fingerprint; /**< 하위 decimal calculation의 scale·rounding·operand provenance fingerprint다. */
    uint64_t replay_fingerprint; /**< Operation input 순서와 canonical block/remainder 결정을 재현하는 audit fingerprint다. */
    uint64_t reserved[3]; /**< 향후 ABI 확장용 세 slot이다. Producer는 모두 0을 기록하고 consumer는 무시한다. */
} math_engine_finance_audit_t;
  • math_engine_finance_audit_t.struct_size

    Caller가 sizeof(math_engine_finance_audit_t) 로 설정하는 byte 크기다.

    uint32_t struct_size;
    
  • math_engine_finance_audit_t.struct_version

    MATH_ENGINE_FINANCE_AUDIT_VERSION_1 인 audit ABI version이다.

    uint32_t struct_version;
    
  • math_engine_finance_audit_t.value

    Operation이 최종 booking한 통화 minor-unit 금액이다.

    math_engine_finance_money_t value;
    
  • math_engine_finance_audit_t.operation

    Booked value를 만든 math_engine_finance_operation_t 값이다.

    uint32_t operation;
    
  • math_engine_finance_audit_t.rounding

    Booking에 적용한 math_engine_decimal_rounding_t 값이다.

    uint32_t rounding;
    
  • math_engine_finance_audit_t.input_scale

    주 입력 decimal amount의 원래 scale이다.

    int32_t input_scale;
    
  • math_engine_finance_audit_t.output_scale

    value.amount.scale 과 같은 booked minor-unit scale이다.

    int32_t output_scale;
    
  • math_engine_finance_audit_t.flags

    MATH_ENGINE_FINANCE_AUDIT_FLAG_* provenance bit의 조합이다.

    uint32_t flags;
    
  • math_engine_finance_audit_t.selected_thread_count

    실제 batch 실행에 선택한 worker 수다. Scalar operation은 1이다.

    uint32_t selected_thread_count;
    
  • math_engine_finance_audit_t.operand_count

    Aggregate/batch/allocation에 소비한 money 또는 weight 원소 수다.

    uint64_t operand_count;
    
  • math_engine_finance_audit_t.period_count

    Interest calculation에 적용한 기간 수다. 다른 operation은 0이다.

    uint64_t period_count;
    
  • math_engine_finance_audit_t.remainder_units

    Allocation/rounding에서 결정적으로 배분하거나 버린 signed minor-unit 수다.

    int64_t remainder_units;
    
  • math_engine_finance_audit_t.decimal_operation_fingerprint

    하위 decimal calculation의 scale·rounding·operand provenance fingerprint다.

    uint64_t decimal_operation_fingerprint;
    
  • math_engine_finance_audit_t.replay_fingerprint

    Operation input 순서와 canonical block/remainder 결정을 재현하는 audit fingerprint다.

    uint64_t replay_fingerprint;
    
  • math_engine_finance_audit_t.reserved

    향후 ABI 확장용 세 slot이다. Producer는 모두 0을 기록하고 consumer는 무시한다.

    uint64_t reserved[3];
    

Macro와 constant

MATH_ENGINE_FINANCE_AUDIT_FLAG_CONSERVED

Allocation 또는 aggregate의 booked minor-unit 총합이 입력 booked 총합과 정확히 같음을 나타낸다.

#define MATH_ENGINE_FINANCE_AUDIT_FLAG_CONSERVED (UINT32_C(1) << 2u)

MATH_ENGINE_FINANCE_AUDIT_FLAG_EXACT

Booked value가 operation의 exact 수학값을 minor-unit에서 손실 없이 표현함을 나타낸다.

#define MATH_ENGINE_FINANCE_AUDIT_FLAG_EXACT (UINT32_C(1) << 0u)

MATH_ENGINE_FINANCE_AUDIT_FLAG_PARALLEL

Batch operation이 둘 이상의 worker thread로 실행됐음을 나타낸다. 결과 순서는 변하지 않는다.

#define MATH_ENGINE_FINANCE_AUDIT_FLAG_PARALLEL (UINT32_C(1) << 3u)

MATH_ENGINE_FINANCE_AUDIT_FLAG_PERIODIC_ROUNDING

Compound interest가 각 기간마다 minor-unit rounding과 booking을 수행했음을 나타낸다.

#define MATH_ENGINE_FINANCE_AUDIT_FLAG_PERIODIC_ROUNDING (UINT32_C(1) << 4u)

MATH_ENGINE_FINANCE_AUDIT_FLAG_ROUNDED

Operation 중 명시 rounding policy로 하나 이상의 remainder를 처리했음을 나타낸다.

#define MATH_ENGINE_FINANCE_AUDIT_FLAG_ROUNDED (UINT32_C(1) << 1u)

MATH_ENGINE_FINANCE_AUDIT_VERSION_1

math_engine_finance_audit_t 의 최초 안정 ABI version이다.

#define MATH_ENGINE_FINANCE_AUDIT_VERSION_1 UINT32_C(1)

MATH_ENGINE_FINANCE_AUDIT_WIRE_SIZE

Canonical little-endian finance audit record 하나의 고정 byte 수다.

#define MATH_ENGINE_FINANCE_AUDIT_WIRE_SIZE UINT64_C(128)

MATH_ENGINE_FINANCE_AUDIT_WIRE_VERSION_1

Canonical finance audit wire format의 최초 안정 version이다.

#define MATH_ENGINE_FINANCE_AUDIT_WIRE_VERSION_1 UINT32_C(1)

MATH_ENGINE_FINANCE_BATCH_BLOCK_ITEMS

Batch replay 순서를 thread 수와 독립적으로 고정하는 논리 block당 item 수다.

#define MATH_ENGINE_FINANCE_BATCH_BLOCK_ITEMS UINT32_C(256)

MATH_ENGINE_FINANCE_MAX_THREADS

Finance batch가 요청할 수 있는 worker thread 수의 product ceiling이다.

#define MATH_ENGINE_FINANCE_MAX_THREADS MATH_ENGINE_DESKTOP_THREAD_COUNT_LIMIT

MATH_ENGINE_FINANCE_POLICY_VERSION_1

math_engine_finance_policy_t 의 최초 안정 ABI version이다.

#define MATH_ENGINE_FINANCE_POLICY_VERSION_1 UINT32_C(1)

Function

math_engine_finance_currency_validate

통화 code와 minor-unit scale을 검증한다.

  • 매개변수 currency: 검사할 통화 값. 세 문자는 모두 대문자 ASCII이고 네 번째 byte는 NUL이어야 한다.
  • 반환: 유효하면 OK, NULL·잘못된 code·18을 넘는 minor unit이면 INVALID_ARGUMENT.
  • Math Engine ownership: currency=borrowed
  • Math Engine nullable: currency=false
  • Math Engine thread_safety: safe
MATH_ENGINE_API math_engine_status_t math_engine_finance_currency_validate(
    const math_engine_finance_currency_t* currency);

math_engine_finance_money_validate

통화와 decimal fixed 금액으로 구성된 money 값을 검증한다.

  • 매개변수 money: 검사할 읽기 전용 값. reserved는 0이어야 한다.
  • 반환: 유효하면 OK, 그렇지 않으면 통화 또는 decimal validator의 오류 status.
  • Math Engine ownership: money=borrowed
  • Math Engine nullable: money=false
  • Math Engine thread_safety: safe
MATH_ENGINE_API math_engine_status_t math_engine_finance_money_validate(
    const math_engine_finance_money_t* money);

math_engine_finance_rate_validate

차원 없는 decimal rate와 rate kind를 검증한다.

  • 매개변수 rate: 검사할 읽기 전용 rate. reserved는 0이어야 한다.
  • 반환: 유효하면 OK, NULL·잘못된 kind 또는 decimal이면 해당 오류 status.
  • Math Engine ownership: rate=borrowed
  • Math Engine nullable: rate=false
  • Math Engine thread_safety: safe
MATH_ENGINE_API math_engine_status_t math_engine_finance_rate_validate(
    const math_engine_finance_rate_t* rate);

math_engine_finance_fx_quote_validate

서로 다른 source/target 통화와 양의 FX rate 및 방향을 검증한다.

  • 매개변수 quote: 검사할 읽기 전용 quote. source와 target 통화는 달라야 한다.
  • 반환: 유효하면 OK, NULL·동일 통화·0 이하 rate·잘못된 방향이면 오류 status.
  • Math Engine ownership: quote=borrowed
  • Math Engine nullable: quote=false
  • Math Engine thread_safety: safe
MATH_ENGINE_API math_engine_status_t math_engine_finance_fx_quote_validate(
    const math_engine_finance_fx_quote_t* quote);

math_engine_finance_workspace_size

count개 비례 배분 또는 batch 임시 항목에 필요한 workspace byte 수를 구한다.

  • 매개변수 count: 처리할 항목 수. 0은 허용하지 않는다.
  • 매개변수 out_size: 성공 시 필요한 byte 수를 받는 호출자 소유 출력. 실패하면 보존된다.
  • 반환: 성공하면 OK, 0개면 INVALID_SIZE, 산술 범위를 넘으면 OUT_OF_RANGE.
  • Math Engine ownership: out_size=caller
  • Math Engine nullable: out_size=false
  • Math Engine thread_safety: safe
MATH_ENGINE_API math_engine_status_t math_engine_finance_workspace_size(
    uint64_t count,
    uint64_t* out_size);

math_engine_finance_money_quantize

money를 통화 minor-unit scale로 한 번 양자화하고 감사 record를 만든다.

  • 매개변수 money: 입력 decimal 금액과 통화.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 반올림과 budget을 담은 읽기 전용 정책.
  • 매개변수 out_audit: 미리 size/version을 설정한 호출자 소유 출력. 실패·취소 시 보존된다.
  • 반환: 성공하면 OK, 검증·반올림·overflow·취소 오류면 해당 status.
  • Math Engine ownership: context=borrowed policy=borrowed out_audit=caller
  • Math Engine nullable: context=false policy=false out_audit=false
  • Math Engine thread_safety: conditional

@details context의 cancel callback을 호출할 수 있으므로 같은 callback state의 동시 사용은 호출자가 동기화한다.

MATH_ENGINE_API math_engine_status_t math_engine_finance_money_quantize(
    math_engine_finance_money_t money,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_money_apply_rate

money에 FEE 또는 TAX rate를 적용해 booked 금액과 감사 record를 계산한다.

  • 매개변수 money: rate를 적용할 금액.
  • 매개변수 rate: 차원 없는 decimal rate.
  • 매개변수 operation: FEE 또는 TAX 중 하나.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 반올림과 budget을 담은 읽기 전용 정책.
  • 매개변수 out_audit: 미리 초기화한 호출자 소유 출력. 실패·취소 시 보존된다.
  • 반환: 성공하면 OK, operation·입력·계산·취소 오류면 해당 status.
  • Math Engine ownership: context=borrowed policy=borrowed out_audit=caller
  • Math Engine nullable: context=false policy=false out_audit=false
  • Math Engine thread_safety: conditional
MATH_ENGINE_API math_engine_status_t math_engine_finance_money_apply_rate(
    math_engine_finance_money_t money,
    math_engine_finance_rate_t rate,
    math_engine_finance_operation_t operation,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_simple_interest

principal * per_period_rate * periods를 계산해 마지막에 한 번 minor-unit으로 반올림한다.

  • 매개변수 principal: 원금과 통화.
  • 매개변수 per_period_rate: kind가 PER_PERIOD인 기간당 decimal rate.
  • 매개변수 periods: 1 이상이며 policy와 context budget 이내인 기간 수.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 반올림과 maximum_periods를 담은 읽기 전용 정책.
  • 매개변수 out_audit: 계산된 단리 금액을 받는 호출자 소유 출력. 실패 시 보존된다.
  • 반환: 성공하면 OK, 기간 범위·입력·overflow·취소 오류면 해당 status.
  • Math Engine ownership: context=borrowed policy=borrowed out_audit=caller
  • Math Engine nullable: context=false policy=false out_audit=false
  • Math Engine thread_safety: conditional
MATH_ENGINE_API math_engine_status_t math_engine_finance_simple_interest(
    math_engine_finance_money_t principal,
    math_engine_finance_rate_t per_period_rate,
    uint64_t periods,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_compound_interest

기간마다 이자를 minor-unit으로 booked하고 balance에 더하는 복리를 계산한다.

  • 매개변수 principal: 시작 원금. 계산 전 통화 minor-unit으로 양자화된다.
  • 매개변수 per_period_rate: kind가 PER_PERIOD인 기간당 decimal rate.
  • 매개변수 periods: 1 이상이며 policy/context의 기간·반복·평가 budget 이내인 기간 수.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 기간별 반올림과 maximum_periods를 담은 읽기 전용 정책.
  • 매개변수 out_audit: 최종 balance와 주기적 반올림 provenance를 받는 출력. 실패 시 보존된다.
  • 반환: 성공하면 OK, budget·입력·계산·취소 오류면 해당 status.
  • Math Engine ownership: context=borrowed policy=borrowed out_audit=caller
  • Math Engine nullable: context=false policy=false out_audit=false
  • Math Engine thread_safety: conditional
MATH_ENGINE_API math_engine_status_t math_engine_finance_compound_interest(
    math_engine_finance_money_t principal,
    math_engine_finance_rate_t per_period_rate,
    uint64_t periods,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_fx_convert

quote 방향에 따라 source 금액을 곱하거나 나누고 target minor-unit으로 양자화한다.

  • 매개변수 source: quote의 source_currency와 일치해야 하는 입력 금액.
  • 매개변수 quote: 서로 다른 두 통화, 양의 rate와 명시적 방향.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: target 양자화 반올림 정책.
  • 매개변수 out_audit: target 통화 금액과 multiply/divide provenance를 받는 출력. 실패 시 보존된다.
  • 반환: 성공하면 OK, 통화 불일치·입력·0 나눗셈·overflow·취소 오류면 해당 status.
  • Math Engine ownership: context=borrowed policy=borrowed out_audit=caller
  • Math Engine nullable: context=false policy=false out_audit=false
  • Math Engine thread_safety: conditional
MATH_ENGINE_API math_engine_status_t math_engine_finance_fx_convert(
    math_engine_finance_money_t source,
    math_engine_finance_fx_quote_t quote,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_aggregate

같은 통화의 count개 금액을 입력 순서와 무관하게 합산한다.

  • 매개변수 values: 같은 통화를 사용하는 count개 읽기 전용 money 배열.
  • 매개변수 count: 1 이상이며 context max_evaluations 이내인 항목 수.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 합산 결과의 반올림 정책.
  • 매개변수 out_audit: 합계와 보존 fingerprint를 받는 출력. 실패 시 보존된다.
  • 반환: 성공하면 OK, 빈 배열·budget·통화 불일치·overflow·취소 오류면 해당 status.
  • Math Engine ownership: values=borrowed context=borrowed policy=borrowed out_audit=caller
  • Math Engine nullable: values=false context=false policy=false out_audit=false
  • Math Engine thread_safety: conditional
MATH_ENGINE_API math_engine_status_t math_engine_finance_aggregate(
    const math_engine_finance_money_t* values,
    uint64_t count,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_allocate_proportional

Booked amount의 모든 minor unit을 largest-remainder 방식으로 보존 배분한다.

  • 매개변수 amount: 통화 minor-unit으로 먼저 양자화할 배분 대상 금액.
  • 매개변수 weights: count개 비음수 정수 가중치. 합은 0이 아니어야 한다.
  • 매개변수 count: 항목 수. context 평가 budget 이내여야 한다.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 반올림과 memory budget 정책.
  • 매개변수 workspace: math_engine_finance_workspace_size() 이상의 caller workspace. data는 8-byte 정렬이어야 한다.
  • 매개변수 outputs: count개 money를 받는 호출자 소유 배열. 실패하면 전 항목이 보존된다.
  • 매개변수 out_audit: 보존 합계와 remainder를 받는 호출자 소유 출력. 실패하면 보존된다.
  • 반환: 성공하면 OK, workspace·budget·가중치·overflow·취소 오류면 해당 status.
  • Math Engine ownership: weights=borrowed context=borrowed policy=borrowed workspace.data=caller outputs=caller out_audit=caller
  • Math Engine nullable: weights=false context=false policy=false workspace.data=false outputs=false out_audit=false
  • Math Engine thread_safety: conditional

@details workspace는 weights 및 outputs 배열과 겹치면 안 된다. 같은 remainder는 낮은 입력 index가 먼저 받고 음수는 magnitude 배분 후 부호를 적용한다.

MATH_ENGINE_API math_engine_status_t math_engine_finance_allocate_proportional(
    math_engine_finance_money_t amount,
    const uint64_t* weights,
    uint64_t count,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_mutable_byte_span_t workspace,
    math_engine_finance_money_t* outputs,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_batch_apply_rate

count개 money에 FEE 또는 TAX를 결정적 256-item block으로 계산한다.

  • 매개변수 values: count개 읽기 전용 입력 배열.
  • 매개변수 count: context 평가 budget 이내인 항목 수.
  • 매개변수 rate: 각 항목에 적용할 decimal rate.
  • 매개변수 operation: FEE 또는 TAX 중 하나.
  • 매개변수 context: FINANCE_FIXED 또는 FULL mode의 읽기 전용 실행 context.
  • 매개변수 policy: 반올림, thread 수와 memory budget 정책. 0 thread는 hardware 수를 선택한다.
  • 매개변수 workspace: math_engine_finance_workspace_size() 이상의 caller workspace.
  • 매개변수 outputs: count개 booked 금액을 받는 배열. 전 항목 성공 후에만 commit된다.
  • 매개변수 out_audit: 합산 결과와 선택 thread 수를 받는 출력. 실패하면 보존된다.
  • 반환: 성공하면 OK, workspace·budget·입력·worker·취소 오류면 해당 status.
  • Math Engine ownership: values=borrowed context=borrowed policy=borrowed workspace.data=caller outputs=caller out_audit=caller
  • Math Engine nullable: values=false context=false policy=false workspace.data=false outputs=false out_audit=false
  • Math Engine thread_safety: conditional

@details workspace는 8-byte 이상 정렬하고 values/outputs와 겹치지 않아야 한다. values와 outputs의 같은 배열 in-place 사용은 지원한다. Block과 output 순서 및 replay fingerprint는 선택 thread 수와 무관하다.

MATH_ENGINE_API math_engine_status_t math_engine_finance_batch_apply_rate(
    const math_engine_finance_money_t* values,
    uint64_t count,
    math_engine_finance_rate_t rate,
    math_engine_finance_operation_t operation,
    const math_engine_context_t* context,
    const math_engine_finance_policy_t* policy,
    math_engine_mutable_byte_span_t workspace,
    math_engine_finance_money_t* outputs,
    math_engine_finance_audit_t* out_audit);

math_engine_finance_audit_encode

감사 record를 locale/host endianness 독립 128-byte little-endian wire로 직렬화한다.

  • 매개변수 audit: 검증할 읽기 전용 감사 record.
  • 매개변수 buffer: 정확히 처음 128 byte에 wire를 받는 호출자 소유 buffer. 실패 시 보존된다.
  • 매개변수 capacity: buffer의 사용 가능한 byte 수. 128 이상이어야 한다.
  • 반환: 성공하면 OK, 잘못된 record면 검증 오류, 작은 buffer면 INSUFFICIENT_BUFFER.
  • Math Engine ownership: audit=borrowed buffer=caller
  • Math Engine nullable: audit=false buffer=false
  • Math Engine thread_safety: safe

@details Wire를 local 임시 배열에서 완성한 뒤 commit하므로 audit storage와 buffer의 overlap을 지원한다.

MATH_ENGINE_API math_engine_status_t math_engine_finance_audit_encode(
    const math_engine_finance_audit_t* audit,
    uint8_t* buffer,
    uint64_t capacity);