Пример #1
0
static void print_dec64(dec64 number) {
    dec64_string_char actual[32];
    if (dec64_is_nan(number) == DEC64_TRUE) {
        printf("nan");
    } else {
        dec64_to_string(state, number, actual);
        printf("%s", actual);
    }
}
Пример #2
0
int dec64_to_string(
    dec64_string_state state,
    dec64 number,
    dec64_string_char string[]
) {
/*
    dec64_to_string converts a dec64 number into a string. The caller provides
    the memory in which to deposit the string. The string must have sufficient
    capacity to hold 32 characters. If NULL is passed in as the string, then
    no characters will be deposited, but a character count will be returned.

    dec64_to_string returns the number of characters actually deposited in the
    string (not including the trailing \0). If the number is nan, then it
    returns 0.

    In standard mode, the number will be formatted conventionally unless it
    would require more than 17 digit, which would be due to excessive
    trailing zeros or zeros immediately after the decimal point. In that
    case scientific notation will be used instead.
*/
    if (state == NULL || state->valid != confirmed) {
        return 0;
    }

    state->length = 0;
    state->string = string;
    if (!dec64_is_nan(number)) {
        if (dec64_is_zero(number)) {
            emit(state, '0');
        } else {
            if (number != state->number) {
                state->number = number;
                digitize(state);
            }
            if (number < 0) {
                emit(state, '-');
            }
            switch (state->mode) {
            case engineering_mode:
                engineering(state);
                break;
            case scientific_mode:
                scientific(state);
                break;
            case standard_mode:
                standard(state);
                break;
            }
        }
    }
    emit_end(state);
    state->string = NULL;
    return state->length;
}
Пример #3
0
void test_is_nan(dec64 first, dec64 expected, char * comment) {
    dec64 actual = dec64_is_nan(first);
    judge_unary(first, expected, actual, "is_nan", "i", comment);
}