example2.c
This file
Path
vendor/decNumber/example2.c
52 lines · 2.4 KiB · C
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than fa6a2ff point at it.
website: remove unused root path on building website (#3248)itchynybefore adoptionfa6a2ff
lines 1–52why, one step back1/* ------------------------------------------------------------------ */
2/* Decimal Number Library Demonstration program */
3/* ------------------------------------------------------------------ */
4/* Copyright (c) IBM Corporation, 2001. All rights reserved. */
5/* ----------------------------------------------------------------+- */
6/* right margin -->| */
7
8// example2.c -- calculate compound interest
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 { // excerpt for User's Guide starts here--------------------------|
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 decNumberFromString(&start, argv[1], &set); // parameter words
37 decNumberFromString(&rate, argv[2], &set);
38 decNumberFromString(&years, argv[3], &set);
39
40 decNumberDivide(&rate, &rate, &hundred, &set); // rate=rate/100
41 decNumberAdd(&rate, &rate, &one, &set); // rate=rate+1
42 decNumberPower(&rate, &rate, &years, &set); // rate=rate^years
43 decNumberMultiply(&total, &rate, &start, &set); // total=rate*start
44 decNumberRescale(&total, &total, &mtwo, &set); // two digits please
45
46 decNumberToString(&total, string);
47 printf("%s at %s%% for %s years => %s\n",
48 argv[1], argv[2], argv[3], string);
49
50 } //---------------------------------------------------------------|
51 return 0;
52 } // main