Skip to content

FloatCompare

FloatCompare

Description

Compare a float against another numeric value. Dispatches on the type of rhs to the matching internal handler.

Parameters

Name Direction Description
lhs in Left-hand float
rhs in Right-hand operand (Float, Int, pointer, integer, or native float type)

Usage example (from documentation)

  int cmp = FloatCompare(&value, 1.5);

Returns

-1 if lhs < rhs, 0 if equal, 1 if lhs > rhs.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    int(FloatCompare)(Float *lhs, Float *rhs) {
        int cmp = 0;
    int MISRA_PRIV_FloatCompareInt(Float *lhs, Int *rhs) {
        Float rhs_value = MISRA_PRIV_FloatFromInt(rhs);
        int   cmp       = FloatCompare(lhs, &rhs_value);
    
        FloatDeinit(&rhs_value);
    int MISRA_PRIV_FloatCompareU64(Float *lhs, u64 rhs) {
        Float rhs_value = MISRA_PRIV_FloatFromU64(rhs);
        int   cmp       = FloatCompare(lhs, &rhs_value);
    
        FloatDeinit(&rhs_value);
    int MISRA_PRIV_FloatCompareI64(Float *lhs, i64 rhs) {
        Float rhs_value = MISRA_PRIV_FloatFromI64(rhs);
        int   cmp       = FloatCompare(lhs, &rhs_value);
    
        FloatDeinit(&rhs_value);
    int MISRA_PRIV_FloatCompareF32(Float *lhs, float rhs) {
        Float rhs_value = float_from_f32_value(rhs);
        int   cmp       = FloatCompare(lhs, &rhs_value);
    
        FloatDeinit(&rhs_value);
    int MISRA_PRIV_FloatCompareF64(Float *lhs, double rhs) {
        Float rhs_value = float_from_f64_value(rhs);
        int   cmp       = FloatCompare(lhs, &rhs_value);
    
        FloatDeinit(&rhs_value);
    
    bool test_float_compare_small_small(void) {
        WriteFmt("Testing FloatCompare with small floats\n");
    
        Float a = FloatFromStr("1.23");
        Float c = FloatFromStr("-1.23");
    
        bool result = FloatCompare(&a, &b) == 0;
        result      = result && FloatEQ(&a, &b);
        result      = result && (FloatCompare(&c, &a) < 0);
        bool result = FloatCompare(&a, &b) == 0;
        result      = result && FloatEQ(&a, &b);
        result      = result && (FloatCompare(&c, &a) < 0);
        result      = result && (FloatCompare(&a, &c) > 0);
        result      = result && FloatEQ(&a, &b);
        result      = result && (FloatCompare(&c, &a) < 0);
        result      = result && (FloatCompare(&a, &c) > 0);
    
        FloatDeinit(&a);
    
    bool test_float_compare_very_large_large(void) {
        WriteFmt("Testing FloatCompare with very large floats\n");
    
        Float a = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
        bool result = FloatLT(&a, &b);
        result      = result && FloatGT(&b, &a);
        result      = result && (FloatCompare(&a, &c) == 0);
        result      = result && FloatEQ(&a, &c);
    
    bool test_float_compare_very_large_small(void) {
        WriteFmt("Testing FloatCompare with very large and small floats\n");
    
        Float large          = FloatFromStr(FLOAT_TEST_VERY_LARGE_ONES);
    
    bool test_float_compare_generic(void) {
        WriteFmt("Testing FloatCompare generic dispatch\n");
    
        Float value = FloatFromStr("12.5");
        Int   next  = IntFrom(13);
    
        bool result = (FloatCompare(&value, same) == 0);
        result      = result && (FloatCompare(&value, whole) > 0);
        result      = result && (FloatCompare(&value, &next) < 0);
    
        bool result = (FloatCompare(&value, same) == 0);
        result      = result && (FloatCompare(&value, whole) > 0);
        result      = result && (FloatCompare(&value, &next) < 0);
        result      = result && (FloatCompare(&value, 12) > 0);
        bool result = (FloatCompare(&value, same) == 0);
        result      = result && (FloatCompare(&value, whole) > 0);
        result      = result && (FloatCompare(&value, &next) < 0);
        result      = result && (FloatCompare(&value, 12) > 0);
        result      = result && (FloatCompare(&value, -1) > 0);
        result      = result && (FloatCompare(&value, whole) > 0);
        result      = result && (FloatCompare(&value, &next) < 0);
        result      = result && (FloatCompare(&value, 12) > 0);
        result      = result && (FloatCompare(&value, -1) > 0);
        result      = result && (FloatCompare(&value, 12.5f) == 0);
        result      = result && (FloatCompare(&value, &next) < 0);
        result      = result && (FloatCompare(&value, 12) > 0);
        result      = result && (FloatCompare(&value, -1) > 0);
        result      = result && (FloatCompare(&value, 12.5f) == 0);
        result      = result && (FloatCompare(&value, 12.5) == 0);
        result      = result && (FloatCompare(&value, 12) > 0);
        result      = result && (FloatCompare(&value, -1) > 0);
        result      = result && (FloatCompare(&value, 12.5f) == 0);
        result      = result && (FloatCompare(&value, 12.5) == 0);
        result      = result && FloatEQ(&value, same);
        result      = result && (FloatCompare(&value, -1) > 0);
        result      = result && (FloatCompare(&value, 12.5f) == 0);
        result      = result && (FloatCompare(&value, 12.5) == 0);
        result      = result && FloatEQ(&value, same);
        result      = result && FloatEQ(&value, 12.5);
    /// TAGS: Float, Compare, Ordering
    ///
    int (FloatCompare)(Float *lhs, Float *rhs);
    #ifndef __cplusplus
    #    define MISRA_FLOAT_COMPARE_DISPATCH(rhs)                                                                          \
                (rhs),                                                                                                     \
                Float: MISRA_PRIV_FloatCompareValueFloat,                                                                  \
                Float *: FloatCompare,                                                                                     \
                const Float *: MISRA_PRIV_FloatCompareConstFloat,                                                          \
                Int: MISRA_PRIV_FloatCompareValueInt,                                                                      \
Last updated on