75 lines
1.8 KiB
C
75 lines
1.8 KiB
C
/* *****************************************************************************
|
|
* bcd/double.c -- make a libg1m BCD out of a double.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libg1m.
|
|
* libg1m is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libg1m is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************** */
|
|
#include <libg1m/internals.h>
|
|
#include <math.h>
|
|
|
|
/**
|
|
* g1m_bcd_fromdouble:
|
|
* Make a libg1m BCD number out of a C-double.
|
|
*
|
|
* @arg dbl the double.
|
|
* @arg bcd the BCD to set.
|
|
*/
|
|
|
|
void g1m_bcd_fromdouble(double dbl, g1m_bcd_t *bcd)
|
|
{
|
|
/* check if is negative */
|
|
int neg = 0;
|
|
if (dbl < 0) {
|
|
neg = 1;
|
|
dbl = -dbl;
|
|
}
|
|
|
|
/* check the exponent, normalize the number */
|
|
int exp = 1;
|
|
while (dbl >= 10) {
|
|
exp++;
|
|
dbl /= 10;
|
|
}
|
|
while (dbl < 1) {
|
|
exp--;
|
|
dbl *= 10;
|
|
}
|
|
|
|
/* get the mantissa */
|
|
for (int i = 0; i < 15; i++) {
|
|
double work = floor(dbl);
|
|
bcd->mant[i] = (int)work;
|
|
dbl = (dbl - work) * 10;
|
|
}
|
|
|
|
/* set the flags */
|
|
bcd->flags = g1m_make_bcdflags(0, neg, 15);
|
|
bcd->exp = exp;
|
|
}
|
|
|
|
/**
|
|
* g1m_bcd_todouble:
|
|
* Make a C-double out of a libg1m BCD number.
|
|
*
|
|
* @arg bcd the BCD to convert.
|
|
* @return the double.
|
|
*/
|
|
|
|
double g1m_bcd_todouble(const g1m_bcd_t *bcd)
|
|
{
|
|
/* TODO */
|
|
return (0);
|
|
}
|