Math Routines

The Math Routines are a compilation of math functions that I have found to be handy for my code.

Math.h

/**************************************************************************************
* Copyright (c) 2010 David Cass Tyler, PO Box 1026, Willard, NM 87063, (505) 384-5342 *
**************************************************************************************/

#ifndef _MATH_H
#define _MATH_H

#if _MSC_VER > 1000
#pragma once
#endif /* _MSC_VER > 1000 */

#ifdef TRACE_HEADERS
#pragma message ( "NOTE:  Included header file:  \"" __FILE__ "\"" )
#endif   /* TRACE_HEADERS */

#ifdef  __cplusplus
extern "C" {
#endif

double Round ( double dblNumber, int nDigits );

#ifdef   __cplusplus
}
#endif

#endif   /* _MATH_H */

Round.c

#include <MATH.H>

/*********************************************************************************************************
*                                                                                                        *
* Copyright (c) 2010 David Cass Tyler, PO Box 1026, Willard, NM 87063, (505) 384-5342                    *
*                                                                                                        *
*     The Round() function rounds a double precision number to the specified number of decimal places.   *
*  It is based on original work by Don Smith of Ktech in Albuquerque.                                    *
*                                                                                                        *
*     A negative value, such as -1.35, actually shows up as -1.3499999999999992 so we round up with a    *
*  slightly larger offset.                                                                               *
*                                                                                                        *
*********************************************************************************************************/

double Round ( double dblNumber, int nDigits )

   {
   double tmp = pow ( 10.0, ( double ) nDigits );

   return ( __int64 ) ( dblNumber * tmp + ( dblNumber > 0.0 ? +0.5 : -0.5000000000001 ) ) / tmp;

   }  /* double Round ( double dblNumber, int nDigits ) */

Try.c

/***************************************************************************************
*                                                                                      *
* Copyright (c) 2010 David Cass Tyler, PO Box 1026, Willard, NM 87063, (505) 384-5342  *
*                                                                                      *
* Compile:  "cl Try.c Round.c"                                                         *
*                                                                                      *
***************************************************************************************/

#include <STDIO.H>

#include "Math.h"

/* Demonstrate the Rounding Function */

void TryRound ( void )

   {
   double i = 0;

   for ( i = -2.01; i < 2.0; i+= 0.11 )
      printf ( "%.4f %.2f\n", i, Round ( i, 1 ) );

   }  /* void main ( void ) */

/* Demonstrate the Math Functions */

void main ( void )

   {

// TryBinomialLeastSquaresFit();

   TryRound();

// TryPolySplineFit();

   }  /* void main ( void ) */