-: 0:Source:mnyfmtdd.c -: 0:Graph:mnyfmtdd.gcno -: 0:Data:mnyfmtdd.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:// (C) Copyright Adolfo Di Mare 2011 -: 2:// Use, modification and distribution are subject to the -: 3:// Boost Software License, Version 1.0. -: 4:// (See accompanying file LICENSE_1_0.txt or copy at -: 5:// http://www.boost.org/LICENSE_1_0.txt) -: 6: -: 7:// Revision history: -: 8:// Oct 2011 Adolfo Di Mare ==> Initial (non boost) version -: 9:// Jun+Ago 2012 Adolfo Di Mare ==> fractpart correction -: 10: -: 11:// mnyfmtdd.c (C) 2011 adolfo@di-mare.com -: 12: -: 13:// Define this macro if your compiler does not have a (long long) data type -: 14:// #define MNYFMT_NO_LONG_LONG -: 15: -: 16:#include "mnyfmt.h" -: 17: -: 18:/** \file mnyfmtdd.c -: 19: \brief Implementation for \c mnyfmtdd(). -: 20: -: 21: \author Adolfo Di Mare -: 22: \date 2011 -: 23:*/ -: 24: -: 25:#ifdef __cplusplus -: 26:// To compile the C source with a C++ compiler, override the file's extension. -: 27:// The GNU gcc compiler does this with option -x: gcc -x c++ -: 28:#endif -: 29: -: 30:// Examples and unit test cases are in in file mnyfmtts.c -: 31: -: 32:/** Wrapper around \c mnyfmt() to use a \c modf() separated double value. -: 33: - Oftentimes, parameters \c intdouble and \c fractdouble are result of \c modf(). -: 34: - Truncates the number of decimals in \c fractdouble to the number of -: 35: decimlas used in the format string \c fmtstr. -: 36: - Ignores the sign in \c fractdouble. -: 37: - Never invokes \c modf(). -: 38: -: 39: \dontinclude mnyfmtts.c -: 40: \skipline test.mnyfmtdd() -: 41: \until }} -: 42: \dontinclude mnyfmtts.c -: 43: \skipline test.modf() -: 44: \until }} -: 45:*/ 6: 46:char* mnyfmtdd(char *fmtstr, char dec, double intdouble, double fractdouble) { -: 47: const char *m; 6: 48: mnyfmt_long intpart = intdouble; -: 49: 6: 50: if ( fractdouble<0.0 ) { 6: 50-block 0 6: 51: fractdouble = -fractdouble; 6: 51-block 0 -: 52: } -: 53: // skip to decimal separator 54: 54: for ( m=fmtstr; (*m)!=dec; ++m) { } 6: 54-block 0 48: 54-block 1 54: 54-block 2 -: 55: 6: 56: if ( *m==0 ) { // no fractional formatting 6: 56-block 0 #####: 57: return mnyfmt( fmtstr, dec, intpart, 0 ); $$$$$: 57-block 0 -: 58: } -: 59: else { -: 60: // count the number of fractional digits in fmtstr 6: 61: ++m; 6: 62: unsigned pow10 = 1; 28: 63: while ( (*m)==mnyfmt_format_char ) { 6: 63-block 0 28: 63-block 1 22: 64: pow10 *= 10; 22: 65: ++m; 22: 65-block 0 -: 66: } -: 67: 6: 68: return mnyfmt( fmtstr, dec, intpart, (unsigned)(fractdouble*pow10) ); 6: 68-block 0 6: 68-block 1 -: 69: } -: 70:} -: 71: -: 72: -: 73:/* THESE ARE NOT AVAILABLE FOR OLD C COMPILERS -: 74: =========================================== -: 75: -: 76: Function: int fesetround (int round) -: 77: http://www.gnu.org/software/libc/manual/html_node/Rounding.html -: 78: -: 79: Changes the currently selected rounding mode to round. If round does not -: 80: correspond to one of the supported rounding modes nothing is changed. -: 81: fesetround returns zero if it changed the rounding mode, a nonzero value -: 82: if the mode is not supported. -: 83: -: 84: You should avoid changing the rounding mode if possible. It can be an -: 85: expensive operation; also, some hardware requires you to compile your -: 86: program differently for it to work. The resulting code may run slower. -: 87: See your compiler documentation for details. -: 88: -: 89: http://www.gnu.org/software/libc/manual/html_node/Floating-Point-Parameters.html -: 90: These macro definitions can be accessed by including the header file -: 91: float.h in your program. -: 92: -: 93: FLT_ROUNDS This value characterizes the rounding mode for floating point addition. -: 94: -: 95: Function: long double rintl (long double x) -: 96: http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html -: 97: -: 98: These functions round x to an integer value according to the current -: 99: rounding mode. See Floating Point Parameters, for information about the -: 100: various rounding modes. The default rounding mode is to round to the -: 101: nearest integer; some machines support other modes, but round-to-nearest -: 102: is always used unless you explicitly select another. -: 103: -: 104: If x was not initially an integer, these functions raise the inexact -: 105: exception. -: 106:*/ -: 107: -: 108:// EOF: mnyfmtdd.c