source: cpp/PrintFloat/Dragon4.h @ 823

Last change on this file since 823 was 823, checked in by Maciej Komosinski, 5 years ago

Added "PrintFloat?" implementation of the Dragon4 algorithm, since C++ still lacks built-in "intelligent" printing of floating point values with full precision

File size: 3.2 KB
Line 
1/******************************************************************************
2  Copyright (c) 2014 Ryan Juckett
3  http://www.ryanjuckett.com/
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14     claim that you wrote the original software. If you use this software
15     in a product, an acknowledgment in the product documentation would be
16     appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19     misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22     distribution.
23******************************************************************************/
24
25#ifndef RJ__Dragon4_h
26#define RJ__Dragon4_h
27
28#include "Standard.h"
29
30//******************************************************************************
31// Different modes for terminating digit output
32//******************************************************************************
33enum tCutoffMode
34{
35    CutoffMode_Unique,          // as many digits as necessary to print a uniquely identifiable number
36    CutoffMode_TotalLength,     // up to cutoffNumber significant digits
37    CutoffMode_FractionLength,  // up to cutoffNumber significant digits past the decimal point
38};
39 
40//******************************************************************************
41// This is an implementation the Dragon4 algorithm to convert a binary number
42// in floating point format to a decimal number in string format. The function
43// returns the number of digits written to the output buffer and the output is
44// not NUL terminated.
45//
46// The floating point input value is (mantissa * 2^exponent).
47//
48// See the following papers for more information on the algorithm:
49//  "How to Print Floating-Point Numbers Accurately"
50//    Steele and White
51//    http://kurtstephens.com/files/p372-steele.pdf
52//  "Printing Floating-Point Numbers Quickly and Accurately"
53//    Burger and Dybvig
54//    http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.72.4656&rep=rep1&type=pdf
55//******************************************************************************
56tU32 Dragon4
57(
58    tU64        mantissa,           // value significand
59    tS32        exponent,           // value exponent in base 2
60    tU32        mantissaHighBitIdx, // index of the highest set mantissa bit
61    tB          hasUnequalMargins,  // is the high margin twice as large as the low margin
62    tCutoffMode cutoffMode,         // how to determine output length
63    tU32        cutoffNumber,       // parameter to the selected cutoffMode
64    tC8 *       pOutBuffer,         // buffer to output into
65    tU32        bufferSize,         // maximum characters that can be printed to pOutBuffer
66    tS32 *      pOutExponent        // the base 10 exponent of the first digit
67);
68
69#endif
Note: See TracBrowser for help on using the repository browser.