//ABEDN calculator;
// MIT License;
//Author - Yuri Spiridonov, ORCID 0000-0002-1260-8908;
//Adder code in C++ for decimal addition of binary numbers
//with double. Implements arithmetic of Binary Equivalents
//of Decimal Numbers (ABEDN). Allows you to sum the binary
//equivalents of decimal floating-point numbers with arbitrary
//signs, including close numbers, with decimal precision. The
//number N <= 15 decimal significant digits to which the answer
//is rounded is specified by the user. The maximum value of the
//mantissa of the input data must be <=9.999999999999999.
//The result is the binary equivalent of the decimal sum
//correctly rounded to N significant digits. The exactly rounded
//decimal number NumberD is displayed in a string like
//NumberD= Sign*C_long E(-k), where Sign is the sign of the number,
//C_long is the integer mantissa, E=10 is the base,
//k is the exponent. For example, if the minus sign, C_long=123, k=5,
//then NumberD=-123E-5. The line also displays the decimal numberB,
//which is equal to the double numberD.
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
double Number,number1,number2,transp,N_C;
long long int C_long;
int N,e,E,k,_C,Sign=1;
N = 15;
number1 = 9.999999999999999e-21;
number2 = -9.999999999999985e-21;
//Check for zero difference;
Number = number1 + number2;
if(Number ==0){
cout<<endl <<"NumberD= "<<0<<'\n';
cout<<endl <<"NumberB= "<<0<<'\n';
return 0;
}
//Checking for the coincidence of the signs of the terms;
if (number1 * number2<0){
// We make the larger number the first term;
if (abs(number1) < abs(number2)){
transp= number2;
number2=number1;
number1=transp;
}
}
//Determining the sign of the sum;
if (Number < 0)
Sign = -1;
//Calculating the exponent of a decimal sum from its binary exponent;
frexp (number1,&e);
E =static_cast <int> ((e-1)*0.301+1);
//Finding a decimal power based on a binary power value:
if (e<1)
E=E-1;
//Calculating the rounding factor;
k=N-E;
// Correction of the rounding factor if necessary.
if (abs(number1)*pow(10,k)>=pow(10,N))
k=k-1;
//Rounding to the nearest integer of the integer
//part of the product of the coefficient 10^k and the
//calculated sum/difference;
C_long = (abs(Number)*pow(10,k)+0.5);
//If the integer part of the sum/difference exceeds the
//allowable value, adjust the integer part and the value
//of the rounding factor;
if (C_long >= pow(10,N)){
C_long=(C_long+5)/10;
k=k-1;
}
//Output the value of the sum/difference, rounded up to N
//significant digits, to a decimal string;
cout<<endl <<"NumberD= "<<Sign*C_long<<"e"<<-k<<'\n';
//Normalization of the difference rounded to N significant digits,
//if necessary;
while (C_long<pow(10,(N-1))){
C_long= (C_long*10);
k=k+1;
}
//Determining the binary equivalent of the rounded sum/difference;
Number=Sign*C_long*pow(10,-k);
//Printing to a string the decimal value of the rounded sum/difference,
//presented in doable format;
cout<<endl <<"NumberB= "<<setprecision(16)<<Number<<'\n';
return 0;
}