cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/include/libg1m/bcd.h

58 lines
2.5 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* libg1m/bcd.h |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libg1m | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2016/11/21 09:23:44 |___/ */
/* */
/* ************************************************************************** */
#ifndef LIBG1M_BCD_H
# define LIBG1M_BCD_H
/* ************************************************************************** */
/* Type definition */
/* ************************************************************************** */
/* BCD are the main number format CASIO uses in its calculators.
* Each semi-byte is a digit going from 0 to 9 (0xA to 0xF aren't used).
* This format has the huge advantage to make 0.1 + 0.2 and 0.3 equal.
*
* The first three digits are the exponent.
* If the exponent is more than 500, then remove 500, and that means the
* number is negative.
* Then you have to remove 100 to have the real exponent.
*
* The other 15 digits are the mantissa. So the number is: (0,M ** E) */
struct bcd {
/* the BCD value itself */
unsigned char BCDval[9];
/* some 4-bytes alignment stuff */
unsigned char _align[3];
};
/* ************************************************************************** */
/* BCD utilities */
/* ************************************************************************** */
/* Arithmetic */
int g1m_bcd_is_nonzero(struct bcd *bcd);
/* String/BCD */
char *g1m_bcdtoa(struct bcd *bcd);
/* ************************************************************************** */
/* Inline BCD utilities */
/* ************************************************************************** */
/* check if BCD number is negative */
static inline int g1m_bcd_is_negative(struct bcd *bcd)
{
return ((bcd->BCDval[0] & 0xf0) >= 0x50);
}
/* macros */
# define g1m_bcd_is_positive(B) (~g1m_bcd_is_negative(B) & 1)
# define g1m_bcd_is_zero(B) (!g1m_bcd_is_nonzero(B))
#endif /* LIBG1M_BCD_H */