MCPcopy Create free account
hub / github.com/Tracktion/choc / writeAndGetEnd

Method writeAndGetEnd

choc/text/choc_FloatToString.h:291–331  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

289 };
290
291 static const char* writeAndGetEnd (char* pos, FloatOrDouble value, int maxDecimalPlaces, bool omitPointIfPossible)
292 {
293 auto startPos = pos;
294 auto floatBits = getFloatBits (value);
295
296 if ((floatBits & signMask) == 0)
297 {
298 if (isZero (floatBits)) return writeZero (pos);
299 }
300 else
301 {
302 pos = write (pos, '-');
303
304 if (isZero (floatBits)) return writeZero (pos);
305
306 value = -value;
307 floatBits &= ~signMask;
308 }
309
310 if (floatBits == nanBits) return write (pos, 'n', 'a', 'n');
311 if (floatBits == infBits) return write (pos, 'i', 'n', 'f');
312
313 auto v = MantissaAndExponent::create (floatBits, floatBits & significandMask);
314 Limits limits (v);
315
316 int K;
317 auto powerOf10 = createPowerOf10 (limits.upper.exponent, K);
318 auto w = powerOf10 * v.normalized();
319 auto upperBound = powerOf10 * limits.upper;
320 upperBound.mantissa--;
321 auto lowerBound = powerOf10 * limits.lower;
322 lowerBound.mantissa++;
323
324 auto totalLength = generateDigits (pos, upperBound, upperBound.mantissa - w.mantissa, upperBound.mantissa - lowerBound.mantissa, K);
325 auto end = addDecimalPointAndExponent (pos, totalLength, K, maxDecimalPlaces < 0 ? defaultNumDecimalPlaces : maxDecimalPlaces);
326
327 if (omitPointIfPossible && end > startPos + 1 && end[-1] == '0' && end[-2] == '.')
328 end -= 2;
329
330 return end;
331 }
332
333 static const char* addDecimalPointAndExponent (char* pos, uint32_t totalLength, int K, int maxDecimalPlaces)
334 {

Callers

nothing calls this directly

Calls 2

normalizedMethod · 0.80
createFunction · 0.70

Tested by

no test coverage detected