diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-11-28 12:44:41 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-11-28 12:44:41 -0800 |
commit | f7b40c2e58d9956e85b6b023129c32296026e6a1 (patch) | |
tree | d01f5b6459e90d47c9093ce635c46f122271878d /lib/Target/CppBackend/CPPBackend.cpp | |
parent | 4542886e6a240aa2ca9b2b4cdf4e9b0d6fe60219 (diff) |
fix emitting of doubles with lots of digits
Diffstat (limited to 'lib/Target/CppBackend/CPPBackend.cpp')
-rw-r--r-- | lib/Target/CppBackend/CPPBackend.cpp | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/Target/CppBackend/CPPBackend.cpp b/lib/Target/CppBackend/CPPBackend.cpp index 1c896bd7f0..b4f84f4fd7 100644 --- a/lib/Target/CppBackend/CPPBackend.cpp +++ b/lib/Target/CppBackend/CPPBackend.cpp @@ -1236,13 +1236,21 @@ std::string CppWriter::getPtr(const Value* Ptr) { } } +// ftostr normally limits output to %20.6e, so some digits can get dropped. We need all the information +static inline std::string ftostr_precise(double V) { + char Buffer[1000]; + sprintf(Buffer, "%f", V); + char *B = Buffer; + while (*B == ' ') ++B; + return B; +} + std::string CppWriter::getConstant(const Constant* CV, Signedness sign) { if (isa<PointerType>(CV->getType())) { - return getPtr(CV); } else { if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { - std::string S = ftostr(CFP->getValueAPF()); + std::string S = ftostr_precise(CFP->getValueAPF().convertToDouble()); S = '+' + S; //if (S.find('.') == S.npos) { TODO: do this when necessary, but it is necessary even for 0.0001 return S; |