Skip to content
Myr

example3.c

This file

Path

vendor/decNumber/example3.c

64 lines · 2.7 KiB · C

Last changed by

Not known.

History of this file

Take it

1/* ------------------------------------------------------------------ */
2/* Decimal Number Library Demonstration program                       */
3/* ------------------------------------------------------------------ */
4/* Copyright (c) IBM Corporation, 2001.  All rights reserved.         */
5/* ----------------------------------------------------------------+- */
6/*                                                 right margin -->|  */
7
8// example3.c -- calculate compound interest, passive checking
9// Arguments are investment, rate (%), and years
10
11#define  DECNUMDIGITS 38           // work with up to 38 digits
12#include "decNumber.h"             // base number library
13#include <stdio.h>                 // for printf
14
15int main(int argc, char *argv[]) {
16  int need=3;
17  if (argc<need+1) {               // not enough words
18    printf("Please supply %d number(s).\n", need);
19    return 1;
20    }
21
22  { // start of Example 2 segment
23  decNumber one, mtwo, hundred;                   // constants
24  decNumber start, rate, years;                   // parameters
25  decNumber total;                                // result
26  decContext set;                                 // working context
27  char string[DECNUMDIGITS+14];                   // conversion buffer
28
29  decContextDefault(&set, DEC_INIT_BASE);         // initialize
30  set.traps=0;                                    // no traps
31  set.digits=25;                                  // precision 25
32  decNumberFromString(&one,       "1", &set);     // set constants
33  decNumberFromString(&mtwo,     "-2", &set);
34  decNumberFromString(&hundred, "100", &set);
35
36// [snip...
37  decNumberFromString(&start, argv[1], &set);     // parameter words
38  decNumberFromString(&rate,  argv[2], &set);
39  decNumberFromString(&years, argv[3], &set);
40  if (set.status & DEC_Errors) {
41    printf("An input argument word was invalid [%s]\n",
42           decContextStatusToString(&set));
43    return 1;
44    }
45  decNumberDivide(&rate, &rate, &hundred, &set);  // rate=rate/100
46  decNumberAdd(&rate, &rate, &one, &set);         // rate=rate+1
47  decNumberPower(&rate, &rate, &years, &set);     // rate=rate^years
48  decNumberMultiply(&total, &rate, &start, &set); // total=rate*start
49  decNumberRescale(&total, &total, &mtwo, &set);  // two digits please
50  if (set.status & DEC_Errors) {
51    set.status &= DEC_Errors;                     // keep only errors
52    printf("Result could not be calculated [%s]\n",
53           decContextStatusToString(&set));
54    return 1;
55    }
56// ...snip]
57
58  decNumberToString(&total, string);
59  printf("%s at %s%% for %s years => %s\n",
60         argv[1], argv[2], argv[3], string);
61
62  } //---------------------------------------------------------------|
63  return 0;
64  } // main