Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
Floats Namespace Reference

Classes

struct  FloatComponents
 
struct  FloatComponents< F32 >
 
struct  FloatComponents< F64 >
 

Functions

template<typename Float >
std::string asString (Float f)
 

Function Documentation

◆ asString()

template<typename Float >
std::string Floats::asString ( Float f)

Definition at line 84 of file Floats.h.

85 {
86 FloatComponents<Float> components;
87 components.value = f;
88
89 auto sign = std::string(components.bits.sign ? "-" : "+");
90
91 if(components.bits.exponent == FloatComponents<Float>::maxExponentBits)
92 {
93 // Handle infinity.
94 if(components.bits.significand == 0) { return sign + "infinity"; }
95 else
96 {
97 // Handle NaN.
98 char significandString[FloatComponents<Float>::numSignificandHexits + 1];
99 for(Uptr hexitIndex = 0;hexitIndex < FloatComponents<Float>::numSignificandHexits;++hexitIndex)
100 {
101 auto hexitValue = char((components.bits.significand >> ((FloatComponents<Float>::numSignificandHexits - hexitIndex - 1) * 4)) & 0xf);
102 significandString[hexitIndex] = hexitValue >= 10 ? ('a' + hexitValue - 10) : ('0' + hexitValue);
103 }
104 significandString[FloatComponents<Float>::numSignificandHexits] = 0;
105 return sign + "nan:0x" + significandString + "";
106 }
107 }
108 else
109 {
110 // If it's not infinite or NaN, just use the STL hexadecimal float printing.
111 char buffer[32];
112 auto numChars = std::sprintf(buffer,FloatComponents<Float>::numSignificandHexits == 6 ? "%.6a" : "%.13a",f);
113 if(unsigned(numChars) + 1 > sizeof(buffer))
114 {
115 Errors::fatal("not enough space in Floats::asString buffer");
116 }
117 return buffer;
118 }
119 }
PointerIntHelper< sizeof(size_t)>::UnsignedIntType Uptr
Definition BasicTypes.h:22
void fatal(const char *message)
Definition Errors.h:21
Here is the call graph for this function:
Here is the caller graph for this function: