Skip to content
Myr

example1.c

This file

Path

vendor/decNumber/example1.c

38 lines · 1.5 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, 2007.  All rights reserved.   */
5/* ----------------------------------------------------------------+- */
6/*                                                 right margin -->|  */
7
8// example1.c -- convert the first two argument words to decNumber,
9// add them together, and display the result
10
11#define  DECNUMDIGITS 34           // work with up to 34 digits
12#include "decNumber.h"             // base number library
13#include <stdio.h>                 // for printf
14
15int main(int argc, char *argv[]) {
16  decNumber a, b;                  // working numbers
17  decContext set;                  // working context
18  char string[DECNUMDIGITS+14];    // conversion buffer
19
20  decContextTestEndian(0);         // warn if DECLITEND is wrong
21
22  if (argc<3) {                    // not enough words
23    printf("Please supply two numbers to add.\n");
24    return 1;
25    }
26  decContextDefault(&set, DEC_INIT_BASE); // initialize
27  set.traps=0;                     // no traps, thank you
28  set.digits=DECNUMDIGITS;         // set precision
29
30  decNumberFromString(&a, argv[1], &set);
31  decNumberFromString(&b, argv[2], &set);
32
33  decNumberAdd(&a, &a, &b, &set);            // a=a+b
34  decNumberToString(&a, string);
35
36  printf("%s + %s => %s\n", argv[1], argv[2], string);
37  return 0;
38  } // main