double-conversion
Efficient binary-decimal and decimal-binary conversion routines for IEEE doubles.
Top Related Projects
Quick Overview
Google's double-conversion library provides efficient binary-to-decimal and decimal-to-binary conversion routines for IEEE doubles and single-precision floats. It aims to be fast, accurate, and platform-independent, making it suitable for various applications requiring high-performance floating-point conversions.
Pros
- High performance and efficiency in floating-point conversions
- Platform-independent implementation
- Well-tested and maintained by Google
- Supports both C and C++ interfaces
Cons
- Limited to IEEE 754 double-precision and single-precision formats
- May require additional integration effort for some projects
- Documentation could be more comprehensive
- Not suitable for arbitrary-precision arithmetic
Code Examples
- Converting a double to a string:
#include "double-conversion/double-conversion.h"
#include <string>
std::string DoubleToString(double value) {
char buffer[128];
double_conversion::DoubleToStringConverter converter(
double_conversion::DoubleToStringConverter::NO_FLAGS,
"Infinity", "NaN", 'e', -6, 21, 6, 0);
double_conversion::StringBuilder builder(buffer, sizeof(buffer));
converter.ToShortest(value, &builder);
return std::string(builder.Finalize());
}
- Converting a string to a double:
#include "double-conversion/double-conversion.h"
double StringToDouble(const char* str, int length) {
double result;
double_conversion::StringToDoubleConverter converter(
double_conversion::StringToDoubleConverter::NO_FLAGS,
0.0, 0.0, "Infinity", "NaN");
int processed_characters_count;
result = converter.StringToDouble(str, length, &processed_characters_count);
return result;
}
- Using the Diy-fp class for custom floating-point operations:
#include "double-conversion/diy-fp.h"
void CustomFloatOperation() {
double_conversion::DiyFp a(0x12345678, 10);
double_conversion::DiyFp b(0x87654321, 5);
double_conversion::DiyFp result = a.Multiply(b);
// result now contains the product of a and b
}
Getting Started
-
Clone the repository:
git clone https://github.com/google/double-conversion.git
-
Include the library in your project:
- For CMake projects, add the following to your CMakeLists.txt:
add_subdirectory(path/to/double-conversion) target_link_libraries(your_target double-conversion)
- For other build systems, add the source files to your project and include the necessary headers.
- For CMake projects, add the following to your CMakeLists.txt:
-
Use the library in your code:
#include "double-conversion/double-conversion.h" // Your code here
Competitor Comparisons
MSVC's implementation of the C++ Standard Library.
Pros of STL
- Comprehensive implementation of the C++ Standard Template Library
- Regularly updated and maintained by Microsoft
- Extensive documentation and community support
Cons of STL
- Larger codebase and potentially more complex to navigate
- May include features beyond basic double conversion functionality
- Potentially higher memory footprint due to broader scope
Code Comparison
STL (from <charconv>
header):
double value;
std::from_chars(str.data(), str.data() + str.size(), value);
double-conversion:
double value;
int processed_chars;
StringToDoubleConverter::StringToDouble(str.c_str(), str.length(), &processed_chars, &value);
Summary
STL offers a comprehensive C++ library implementation, while double-conversion focuses specifically on efficient number-to-string and string-to-number conversions. STL provides broader functionality but may be more complex, whereas double-conversion is more specialized and potentially easier to integrate for specific conversion tasks. The code comparison shows that STL uses a more modern C++ approach with std::from_chars
, while double-conversion uses a custom converter class.
Abseil Common Libraries (C++)
Pros of abseil-cpp
- Broader scope: Offers a comprehensive set of C++ library components
- Active development: More frequent updates and contributions
- Extensive documentation and examples
Cons of abseil-cpp
- Larger codebase: May increase compilation time and binary size
- Steeper learning curve due to its extensive feature set
Code Comparison
abseil-cpp:
#include "absl/strings/str_cat.h"
#include "absl/time/time.h"
std::string result = absl::StrCat("Time: ", absl::FormatTime(absl::Now()));
double-conversion:
#include "double-conversion/double-conversion.h"
char buffer[128];
double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToFixed(3.14159, 2, buffer, sizeof(buffer));
Summary
abseil-cpp is a more comprehensive C++ library with a wider range of features, while double-conversion focuses specifically on efficient number-to-string and string-to-number conversions. abseil-cpp offers more functionality but comes with a larger footprint, whereas double-conversion is lightweight and specialized. The choice between the two depends on project requirements and the need for additional C++ utilities beyond number conversions.
Super-project for modularized Boost
Pros of Boost
- Comprehensive library collection: Boost offers a wide range of C++ libraries for various purposes, not limited to just number conversion
- Well-established and widely used: Boost has a large community and is often considered a de facto standard in C++ development
- Regular updates and maintenance: Boost is actively developed and maintained by a dedicated team
Cons of Boost
- Large footprint: Including Boost in a project can significantly increase its size, which may be unnecessary for simple conversion tasks
- Steeper learning curve: Due to its extensive feature set, Boost can be more complex to learn and use compared to more focused libraries
- Potential overhead: Some Boost libraries may introduce performance overhead for simple operations
Code Comparison
double-conversion:
#include <double-conversion/double-conversion.h>
double_conversion::DoubleToStringConverter converter(flags, infinity_symbol, nan_symbol, exponent_character, decimal_in_shortest_low, decimal_in_shortest_high, max_leading_padding_zeroes_in_precision_mode, max_trailing_padding_zeroes_in_precision_mode);
char buffer[128];
double_conversion::StringBuilder builder(buffer, sizeof(buffer));
converter.ToShortest(value, &builder);
Boost:
#include <boost/lexical_cast.hpp>
std::string result = boost::lexical_cast<std::string>(value);
A modern formatting library
Pros of fmt
- Broader functionality: fmt is a comprehensive formatting library, while double-conversion focuses solely on floating-point conversion
- Modern C++ design: fmt utilizes C++11 features and beyond, offering a more contemporary API
- Active development: fmt has more frequent updates and a larger community
Cons of fmt
- Larger library size: fmt includes more features, potentially increasing binary size
- Learning curve: fmt's extensive API may require more time to master compared to double-conversion's focused functionality
Code Comparison
fmt:
#include <fmt/core.h>
std::string s = fmt::format("{:.2f}", 3.14159);
double-conversion:
#include <double-conversion/double-conversion.h>
char buffer[32];
double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToFixed(3.14159, 2, buffer, sizeof(buffer));
Summary
fmt is a versatile formatting library with a modern C++ design, while double-conversion specializes in efficient floating-point conversions. fmt offers more features but may have a larger footprint, whereas double-conversion provides a focused solution for specific use cases. Choose based on your project's requirements and performance constraints.
JSON for Modern C++
Pros of json
- Broader functionality: json is a full-featured JSON library, while double-conversion focuses solely on number conversion
- More active development: json has more recent updates and a larger community
- Header-only library: Easier integration into projects
Cons of json
- Larger codebase: More complex and potentially slower for specific number conversion tasks
- Higher memory usage: Due to its comprehensive JSON handling capabilities
Code Comparison
json:
#include <nlohmann/json.hpp>
using json = nlohmann::json;
json j = {{"pi", 3.141}, {"happy", true}};
std::string s = j.dump();
double-conversion:
#include <double-conversion/double-conversion.h>
char buffer[128];
double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToFixed(3.141, 3, buffer, sizeof(buffer));
While json provides a more comprehensive JSON handling solution, double-conversion offers a lightweight and specialized approach for number conversion. The choice between the two depends on the specific requirements of the project, with json being more suitable for general JSON processing and double-conversion excelling in efficient number-to-string conversions.
Fast C++ logging library.
Pros of spdlog
- Comprehensive logging functionality with multiple sinks and formatters
- Header-only library, making it easy to integrate into projects
- Supports asynchronous logging for improved performance
Cons of spdlog
- Larger codebase and potentially higher memory footprint
- May have a steeper learning curve due to more features and options
Code Comparison
spdlog:
#include "spdlog/spdlog.h"
int main() {
spdlog::info("Welcome to spdlog!");
spdlog::error("An error message with format: {}", 42);
return 0;
}
double-conversion:
#include "double-conversion/double-conversion.h"
int main() {
double_conversion::DoubleToStringConverter converter(flags, infinity_symbol, nan_symbol, 10, 0, 0, 0, 0);
char buffer[128];
double_conversion::StringBuilder builder(buffer, sizeof(buffer));
converter.ToShortest(3.14159, &builder);
return 0;
}
Summary
spdlog is a feature-rich logging library, while double-conversion focuses specifically on efficient number-to-string and string-to-number conversions. spdlog offers more comprehensive logging capabilities but may be overkill for projects that only need simple number formatting. double-conversion is more specialized and lightweight, making it a better choice for projects primarily concerned with efficient numeric conversions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Double Conversion
https://github.com/google/double-conversion
This project (double-conversion) provides binary-decimal and decimal-binary routines for IEEE doubles.
The library consists of efficient conversion routines that have been extracted from the V8 JavaScript engine. The code has been refactored and improved so that it can be used more easily in other projects.
There is extensive documentation in double-conversion/string-to-double.h
and
double-conversion/double-to-string.h
. Other examples can be found in
test/cctest/test-conversions.cc
.
Building
This library can be built with scons or cmake. The checked-in Makefile simply forwards to scons, and provides a shortcut to run all tests:
make
make test
Scons
The easiest way to install this library is to use scons
. It builds
the static and shared library, and is set up to install those at the
correct locations:
scons install
Use the DESTDIR
option to change the target directory:
scons DESTDIR=alternative_directory install
Cmake
To use cmake run cmake .
in the root directory. This overwrites the
existing Makefile.
Use -DBUILD_SHARED_LIBS=ON
to enable the compilation of shared libraries.
Note that this disables static libraries. There is currently no way to
build both libraries at the same time with cmake.
Use -DBUILD_TESTING=ON
to build the test executable.
cmake . -DBUILD_TESTING=ON
make
test/cctest/cctest
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot