[cig-commits] r4135 - in long/3D/Gale/trunk/src/StGermain: .
Base/Foundation/src Base/Foundation/tests
walter at geodynamics.org
walter at geodynamics.org
Tue Aug 1 01:53:02 PDT 2006
Author: walter
Date: 2006-08-01 01:53:01 -0700 (Tue, 01 Aug 2006)
New Revision: 4135
Modified:
long/3D/Gale/trunk/src/StGermain/
long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.c
long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.h
long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.0of1.expected
long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.c
Log:
r2601 at earth: boo | 2006-08-01 01:50:31 -0700
r2580 at earth (orig r3703): PatrickSunter | 2006-07-23 23:25:21 -0700
Added new functions to the "CommonRoutines" general function
toolbox in StGermain/Base/Foundation:
* /** Rounds a double to a certain number of decimal places */
double StG_RoundDoubleToNDecimalPlaces( double value, unsigned int nDecimalPlaces );
/** Rounds a double to the specified number of significant figures */
double StG_RoundDoubleToNSigFigs( double value, unsigned int nSigFigs );
I couldn't find them in the C std library, and other people online seemed to have
written their own too. Added corresponding tests in the tests dir.
Property changes on: long/3D/Gale/trunk/src/StGermain
___________________________________________________________________
Name: svk:merge
- 1ef209d2-b310-0410-a72d-e20c9eb0015c:/cig:2600
afb6c753-b9d0-0310-b4e7-dbd8d91cdd35:/trunk/StGermain:3702
+ 1ef209d2-b310-0410-a72d-e20c9eb0015c:/cig:2601
afb6c753-b9d0-0310-b4e7-dbd8d91cdd35:/trunk/StGermain:3703
Modified: long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.c 2006-08-01 08:52:58 UTC (rev 4134)
+++ long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.c 2006-08-01 08:53:01 UTC (rev 4135)
@@ -43,9 +43,36 @@
#include <stdarg.h>
#include <stddef.h>
#include <ctype.h>
+#include <math.h>
const char* StG_BoolToStringMap[2] = { "False", "True" };
+
+double StG_RoundDoubleToNSigFigs( double value, unsigned int nSigFigs ) {
+ double divisorPower;
+
+ assert( nSigFigs >= 1 );
+
+ /* Since interested in significant figures, need to work out the number of
+ zeros in the answer */
+ divisorPower = log10( value );
+ divisorPower = floor( divisorPower );
+
+ /* We now have the divisorPower to use if nSigFigs == 1. So adjust if this isn't
+ the case */
+ divisorPower -= ( nSigFigs - 1 );
+
+ /* Similar approach to the decimal places rounder from here, except we use
+ the divisorPower we've just calculated */
+ return floor( value / pow( 10., divisorPower ) + 0.5 ) * pow(10., divisorPower);
+}
+
+
+double StG_RoundDoubleToNDecimalPlaces( double value, unsigned int nDecimalPlaces ) {
+ return floor( value * pow(10., nDecimalPlaces) + .5) / pow(10., nDecimalPlaces);
+}
+
+
unsigned int StG_IntegerLength( int number ) {
int tmpNumber;
int numDigits;
Modified: long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.h
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.h 2006-08-01 08:52:58 UTC (rev 4134)
+++ long/3D/Gale/trunk/src/StGermain/Base/Foundation/src/CommonRoutines.h 2006-08-01 08:53:01 UTC (rev 4135)
@@ -45,6 +45,12 @@
/** Global map from a Boolean's enum value to a string: for printing purposes */
extern const char* StG_BoolToStringMap[2];
+ /** Rounds a double to the specified number of significant figures */
+ double StG_RoundDoubleToNSigFigs( double value, unsigned int nSigFigs );
+
+ /** Rounds a double to a certain number of decimal places */
+ double StG_RoundDoubleToNDecimalPlaces( double value, unsigned int nDecimalPlaces );
+
/** Counts the number of characters required to display the given base 10 value. */
unsigned int StG_IntegerLength( int number );
Modified: long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.0of1.expected
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.0of1.expected 2006-08-01 08:52:58 UTC (rev 4134)
+++ long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.0of1.expected 2006-08-01 08:53:01 UTC (rev 4135)
@@ -17,3 +17,42 @@
' ) = True
Stg_StringIsEmpty( ' sdf
' ) = False
+
+Testing rounding to certain number of decimal places:
+Value 9.7324 rounded to 0 dec places was within tolerance of expected value 10.
+Value 9.7324 rounded to 1 dec places was within tolerance of expected value 9.7.
+Value 9.7324 rounded to 2 dec places was within tolerance of expected value 9.73.
+Value 9.7324 rounded to 3 dec places was within tolerance of expected value 9.732.
+Value 97.654 rounded to 0 dec places was within tolerance of expected value 98.
+Value 97.654 rounded to 1 dec places was within tolerance of expected value 97.7.
+Value 97.654 rounded to 2 dec places was within tolerance of expected value 97.65.
+Value 97.654 rounded to 3 dec places was within tolerance of expected value 97.654.
+Value 104.321 rounded to 0 dec places was within tolerance of expected value 104.
+Value 104.321 rounded to 1 dec places was within tolerance of expected value 104.3.
+Value 104.321 rounded to 2 dec places was within tolerance of expected value 104.32.
+Value 104.321 rounded to 3 dec places was within tolerance of expected value 104.321.
+Value 13762.1 rounded to 0 dec places was within tolerance of expected value 13762.
+Value 13762.1 rounded to 1 dec places was within tolerance of expected value 13762.1.
+Value 13762.1 rounded to 2 dec places was within tolerance of expected value 13762.1.
+Value 13762.1 rounded to 3 dec places was within tolerance of expected value 13762.1.
+Value 0.0043253 rounded to 0 dec places was within tolerance of expected value 0.
+Value 0.0043253 rounded to 1 dec places was within tolerance of expected value 0.
+Value 0.0043253 rounded to 2 dec places was within tolerance of expected value 0.
+Value 0.0043253 rounded to 3 dec places was within tolerance of expected value 0.004.
+
+Testing rounding to certain number of significant figures:
+Value 9.7324 rounded to 1 sig. figures was within tolerance of expected value 10.
+Value 9.7324 rounded to 2 sig. figures was within tolerance of expected value 9.7.
+Value 9.7324 rounded to 3 sig. figures was within tolerance of expected value 9.73.
+Value 97.654 rounded to 1 sig. figures was within tolerance of expected value 100.
+Value 97.654 rounded to 2 sig. figures was within tolerance of expected value 98.
+Value 97.654 rounded to 3 sig. figures was within tolerance of expected value 97.7.
+Value 104.321 rounded to 1 sig. figures was within tolerance of expected value 100.
+Value 104.321 rounded to 2 sig. figures was within tolerance of expected value 100.
+Value 104.321 rounded to 3 sig. figures was within tolerance of expected value 104.
+Value 13762.1 rounded to 1 sig. figures was within tolerance of expected value 10000.
+Value 13762.1 rounded to 2 sig. figures was within tolerance of expected value 14000.
+Value 13762.1 rounded to 3 sig. figures was within tolerance of expected value 13800.
+Value 0.0043253 rounded to 1 sig. figures was within tolerance of expected value 0.004.
+Value 0.0043253 rounded to 2 sig. figures was within tolerance of expected value 0.0043.
+Value 0.0043253 rounded to 3 sig. figures was within tolerance of expected value 0.00433.
Modified: long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.c
===================================================================
--- long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.c 2006-08-01 08:52:58 UTC (rev 4134)
+++ long/3D/Gale/trunk/src/StGermain/Base/Foundation/tests/testCommonRoutines.c 2006-08-01 08:53:01 UTC (rev 4135)
@@ -38,6 +38,7 @@
#include <stdarg.h>
#include <mpi.h>
#include <string.h>
+#include <math.h>
void TestLMS( Stream* stream, char* string1, char* string2, Bool caseSensitive ) {
Journal_Printf( stream, "Longest case-%ssensitive matching subsequence length of '%s' and '%s' is %d.\n",
@@ -80,6 +81,32 @@
}
if ( rank == procToWatch ) {
+ Index nTestValues = 5;
+ double testValues[] = {
+ 9.7324,
+ 97.654,
+ 104.321,
+ 13762.1,
+ 0.0043253 };
+ double expectedRoundedToDecPlaces[5][4] = {
+ { 10., 9.7, 9.73, 9.732 },
+ { 98., 97.7, 97.65, 97.654 },
+ { 104., 104.3, 104.32, 104.321 },
+ { 13762., 13762.1, 13762.10, 13762.100 },
+ { 0., 0.0, 0.00, 0.004 } };
+ double expectedRoundedToSigFigs[5][4] = {
+ { 0., 10, 9.7, 9.73 },
+ { 0., 100, 98, 97.7 },
+ { 0., 100, 100, 104 },
+ { 0., 10000, 14000, 13800 },
+ { 0., 0.004, 0.0043, 0.00433 } };
+ double roundedValue;
+ double errorMargin;
+ Index testValue_I;
+ Index nDecPlaces;
+ Index nSigFigs;
+ double tolerance = 1e-12;
+
TestLMS( stream, "Acrobat", "BOAT", True );
TestLMS( stream, "Abracadabra", "Yabbadabbadoo", True );
TestLMS( stream, "Abracadabra", "Yabbadabbadoo", False );
@@ -98,6 +125,52 @@
TestIsStringEmpty( stream, " " );
TestIsStringEmpty( stream, " \n" );
TestIsStringEmpty( stream, " sdf \n" );
+
+ printf("\nTesting rounding to certain number of decimal places:\n");
+ for ( testValue_I = 0; testValue_I < nTestValues; testValue_I++ ) {
+ for ( nDecPlaces = 0; nDecPlaces <=3; nDecPlaces++ ) {
+ roundedValue = StG_RoundDoubleToNDecimalPlaces(
+ testValues[testValue_I], nDecPlaces );
+ errorMargin = fabs( roundedValue -
+ expectedRoundedToDecPlaces[testValue_I][nDecPlaces] );
+ if ( errorMargin <= tolerance ) {
+ printf( "Value %8g rounded to %u dec places was within "
+ "tolerance of expected value %8g.\n",
+ testValues[testValue_I], nDecPlaces,
+ expectedRoundedToDecPlaces[testValue_I][nDecPlaces] );
+ }
+ else {
+ printf( "Value %8g rounded to %u dec places was not within "
+ "tolerance of expected value %8g.\n",
+ testValues[testValue_I], nDecPlaces,
+ expectedRoundedToDecPlaces[testValue_I][nDecPlaces] );
+ printf( "(error margin was %.8g)\n", errorMargin );
+ }
+ }
+ }
+
+ printf("\nTesting rounding to certain number of significant figures:\n");
+ for ( testValue_I = 0; testValue_I < nTestValues; testValue_I++ ) {
+ for ( nSigFigs = 1; nSigFigs <=3; nSigFigs++ ) {
+ roundedValue = StG_RoundDoubleToNSigFigs(
+ testValues[testValue_I], nSigFigs );
+ errorMargin = fabs( roundedValue -
+ expectedRoundedToSigFigs[testValue_I][nSigFigs] );
+ if ( errorMargin <= tolerance ) {
+ printf( "Value %8g rounded to %u sig. figures was within "
+ "tolerance of expected value %8g.\n",
+ testValues[testValue_I], nSigFigs,
+ expectedRoundedToSigFigs[testValue_I][nSigFigs] );
+ }
+ else {
+ printf( "Value %8g rounded to %u sig. figures was not within "
+ "tolerance of expected value %8g.\n",
+ testValues[testValue_I], nSigFigs,
+ expectedRoundedToSigFigs[testValue_I][nSigFigs] );
+ printf( "(error margin was %.8g)\n", errorMargin );
+ }
+ }
+ }
}
RegressionTest_Finalise();
More information about the cig-commits
mailing list