Distinguish float from double

This commit is contained in:
Orivej Desh 2019-10-12 02:47:59 +00:00 committed by Victor Zverovich
parent a927dda9bb
commit b87ac4d840
4 changed files with 20 additions and 5 deletions

View File

@ -657,6 +657,7 @@ enum type {
char_type,
last_integer_type = char_type,
// followed by floating-point types.
float_type,
double_type,
long_double_type,
last_numeric_type = long_double_type,
@ -683,6 +684,7 @@ FMT_TYPE_CONSTANT(int128_t, int128_type);
FMT_TYPE_CONSTANT(uint128_t, uint128_type);
FMT_TYPE_CONSTANT(bool, bool_type);
FMT_TYPE_CONSTANT(Char, char_type);
FMT_TYPE_CONSTANT(float, float_type);
FMT_TYPE_CONSTANT(double, double_type);
FMT_TYPE_CONSTANT(long double, long_double_type);
FMT_TYPE_CONSTANT(const Char*, cstring_type);
@ -724,6 +726,7 @@ template <typename Context> class value {
uint128_t uint128_value;
bool bool_value;
char_type char_value;
float float_value;
double double_value;
long double long_double_value;
const void* pointer;
@ -738,6 +741,7 @@ template <typename Context> class value {
value(unsigned long long val) : ulong_long_value(val) {}
value(int128_t val) : int128_value(val) {}
value(uint128_t val) : uint128_value(val) {}
value(float val) : float_value(val) {}
value(double val) : double_value(val) {}
value(long double val) : long_double_value(val) {}
value(bool val) : bool_value(val) {}
@ -809,7 +813,7 @@ template <typename Context> struct arg_mapper {
return val;
}
FMT_CONSTEXPR double map(float val) { return static_cast<double>(val); }
FMT_CONSTEXPR float map(float val) { return val; }
FMT_CONSTEXPR double map(double val) { return val; }
FMT_CONSTEXPR long double map(long double val) { return val; }
@ -985,6 +989,8 @@ FMT_CONSTEXPR auto visit_format_arg(Visitor&& vis,
return vis(arg.value_.bool_value);
case internal::char_type:
return vis(arg.value_.char_value);
case internal::float_type:
return vis(arg.value_.float_value);
case internal::double_type:
return vis(arg.value_.double_value);
case internal::long_double_type:

View File

@ -1003,6 +1003,13 @@ FMT_API bool grisu_format(Double value, buffer<char>& buf, int precision,
return true;
}
template <>
char* sprintf_format<float>(float value, internal::buffer<char>& buf,
sprintf_specs specs) {
// printf does not have a float format modifier, it only supports double.
return sprintf_format<double>(value, buf, specs);
}
template <typename Double>
char* sprintf_format(Double value, internal::buffer<char>& buf,
sprintf_specs specs) {

View File

@ -1663,6 +1663,10 @@ template <typename Range> class basic_writer {
int_writer<T, Spec>(*this, value, spec));
}
void write(float value, const format_specs& specs = format_specs()) {
write_double(value, specs);
}
void write(double value, const format_specs& specs = format_specs()) {
write_double(value, specs);
}
@ -1677,7 +1681,7 @@ template <typename Range> class basic_writer {
write_double(value, specs);
}
// Formats a floating-point number (double or long double).
// Formats a floating-point number (float, double, or long double).
template <typename T, bool USE_GRISU = fmt::internal::use_grisu<T>()>
void write_double(T value, const format_specs& specs);
@ -3006,6 +3010,7 @@ struct formatter<T, Char,
handle_char_specs(
&specs_, internal::char_specs_checker<decltype(eh)>(specs_.type, eh));
break;
case internal::float_type:
case internal::double_type:
case internal::long_double_type:
handle_float_type_spec(specs_.type,
@ -3061,7 +3066,6 @@ FMT_FORMAT_AS(short, int);
FMT_FORMAT_AS(unsigned short, unsigned);
FMT_FORMAT_AS(long, long long);
FMT_FORMAT_AS(unsigned long, unsigned long long);
FMT_FORMAT_AS(float, double);
FMT_FORMAT_AS(Char*, const Char*);
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(std::nullptr_t, const void*);

View File

@ -290,8 +290,6 @@ VISIT_TYPE(long, long long);
VISIT_TYPE(unsigned long, unsigned long long);
#endif
VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) \
{ \
testing::StrictMock<mock_visitor<decltype(expected)>> visitor; \