예제 #1
0
void reset_floatfield_flags_assuming_hexfloat_state(Writer &dout, std::ios_base::fmtflags reset_flags)
{
    if ((reset_flags & std::ios_base::scientific) && !(reset_flags & std::ios_base::fixed))
        fixed(dout);
    else if ((reset_flags & std::ios_base::fixed) && !(reset_flags & std::ios_base::scientific))
        scientific(dout);
}
예제 #2
0
static void standard(
    dec64_string_state state
) {
    int from = 0;
    int to;
    int places;
    int sep = 0;
    int64 exponent = dec64_exponent(state->number);
    if (exponent >= 0) {
        to = state->nr_digits + (int)exponent;
        if (to + state->places > 20) {
            scientific(state);
        } else {
            emit_digits_separated(state, 0, to);
            if (state->places > 0) {
                emit_decimal_point(state);
                emit_digits(state, to, state->places + to);
            }
        }
    } else {
        from = (int)exponent + state->nr_digits;
        to = state->nr_digits - state->nr_zeros;
        if (from <= 0) {
            places = to - from;
            if (places > 18) {
                scientific(state);
            } else {
                emit(state, '0');
                emit_decimal_point(state);
                if (places < state->places) {
                    to = state->places + from;
                }
                emit_digits(state, from, to);
            }
        } else {
            emit_digits_separated(state, 0, from);
            emit_decimal_point(state);
            if (to - from < state->places) {
                to = state->places + from;
            }
            emit_digits(state, from, to);
        }
    }
}
예제 #3
0
bool is_about(double x, double y, int p)
{
    std::ostringstream o;
    o.precision(p);
    scientific(o);
    o << x;
    std::string a = o.str();
    o.str("");
    o << y;
    return a == o.str();
}
예제 #4
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 indicating an empty string.

    In standard mode, the number will be formatted conventionally unless it
    would require more than 17 digits, 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_any_nan(number) != DEC64_TRUE) {
        if (dec64_is_zero(number) == DEC64_TRUE) {
            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;
}