1994-08-12 10:52:49 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <gm.h>
|
1994-08-17 09:15:44 +00:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
double pow (double, double);
|
1994-08-17 09:32:34 +00:00
|
|
|
} // Should be #include <math.h>
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
#include <strings.h>
|
|
|
|
#include <real.h>
|
|
|
|
|
|
|
|
HIDDEN real __tmp_real;
|
|
|
|
HIDDEN char __string[80];
|
1994-08-17 09:15:44 +00:00
|
|
|
const real ZERO (0.0);
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real::real ()
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dzero (ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real::real (const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dcpy (ptr (), b.ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real::real (double a)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:32:34 +00:00
|
|
|
dftodr (ptr (), a, 9); // Round the number (1.0 is NOT 0.999999999)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:32:34 +00:00
|
|
|
deltrz (ptr (), ptr ()); // Delete Trailing zeroes
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
char *real::eng2ita (char *s)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
if (s)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
char *dot = strchr (s, '.');
|
|
|
|
if (dot)
|
|
|
|
*dot = ',';
|
|
|
|
}
|
1994-08-17 09:15:44 +00:00
|
|
|
return s;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
char *real::ita2eng (const char *s)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
int j = 0;
|
|
|
|
if (s)
|
|
|
|
for (int i = 0; s[i]; i++)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
switch (s[i])
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
case ' ':
|
|
|
|
case '.':
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
__string[j++] = '.';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
__string[j++] = s[i];
|
|
|
|
break;
|
1994-08-17 09:15:44 +00:00
|
|
|
}
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-17 09:15:44 +00:00
|
|
|
__string[j] = '\0';
|
|
|
|
return __string;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
bool real::is_real (const char *s)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
bool ok = FALSE;
|
|
|
|
if (s)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
while (*s == ' ')
|
|
|
|
s++; // Remove leading spaces before atod
|
1994-08-17 14:07:28 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
ok = atod (__tmp_real.ptr (), (char *) s) != GM_NULL;
|
|
|
|
}
|
1994-08-17 09:15:44 +00:00
|
|
|
return ok;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-12-07 13:38:30 +00:00
|
|
|
bool real::is_natural (const char *s)
|
|
|
|
{
|
|
|
|
bool ok = s && *s != '\0';
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
while (*s == ' ')
|
|
|
|
s++; // Remove leading spaces before
|
|
|
|
|
|
|
|
if (*s)
|
|
|
|
{
|
|
|
|
while (isdigit(*s))
|
|
|
|
s++;
|
|
|
|
ok = *s == '\0';
|
|
|
|
}
|
|
|
|
else ok = FALSE;
|
|
|
|
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real::real (const char *s)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
if (s)
|
|
|
|
while (*s == ' ')
|
1994-08-17 09:32:34 +00:00
|
|
|
s++; // Remove leading spaces before atod
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
if (s && *s)
|
|
|
|
atod (ptr (), (char *) s);
|
|
|
|
else
|
|
|
|
dzero (ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator = (const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dcpy (ptr (), b.ptr ());
|
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator = (double a)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
const real n (a);
|
|
|
|
operator = (n);
|
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator += (const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dadd (ptr (), ptr (), b.ptr ());
|
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator += (double a)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-12-02 13:30:45 +00:00
|
|
|
adddfd (ptr (), ptr (), a);
|
1994-08-17 09:15:44 +00:00
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator -= (const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-12-02 13:30:45 +00:00
|
|
|
dsub (ptr (), ptr (), b.ptr ());
|
1994-08-17 09:15:44 +00:00
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator *= (const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dmul (ptr (), ptr (), b.ptr ());
|
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real & real::operator /= (const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
const DEC *dst = ddiv (ptr (), ptr (), b.ptr ());
|
1994-08-16 10:50:19 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
#ifdef DBG
|
|
|
|
if (dst == GM_NULL)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
errname (__string, gmec ());
|
|
|
|
error_box ("Division error: %s", __string);
|
|
|
|
}
|
1994-08-17 09:15:44 +00:00
|
|
|
#endif
|
1994-08-16 10:50:19 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
return *this;
|
1994-08-17 14:07:28 +00:00
|
|
|
}
|
1994-08-17 10:36:55 +00:00
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
TObject *real:: dup () const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
return new real (*this);
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
bool real::is_zero () const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
return diszero (ptr ());
|
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
int real::sign () const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
return dsign (ptr ());
|
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
real real::operator - () const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
real n;
|
|
|
|
dchgs (n.ptr (), ptr ());
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
1994-09-19 09:50:07 +00:00
|
|
|
long real::integer () const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-09-19 09:50:07 +00:00
|
|
|
return (long)dtodf(ptr ());
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
// Certified 91%
|
1994-09-19 09:50:07 +00:00
|
|
|
char *real::string (int len, int dec, char pad) const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
__tmp_real = *this;
|
|
|
|
if (dec != UNDEFINED)
|
|
|
|
__tmp_real.round (dec);
|
|
|
|
else
|
|
|
|
deltrz (__tmp_real.ptr (), __tmp_real.ptr ());
|
|
|
|
|
|
|
|
dtoa (__string, __tmp_real.ptr ());
|
|
|
|
int lun = strlen (__string);
|
|
|
|
|
|
|
|
if (lun < len)
|
|
|
|
{
|
|
|
|
const int delta = len - lun;
|
|
|
|
for (int i = lun; i >= 0; i--)
|
|
|
|
__string[i + delta] = __string[i];
|
|
|
|
for (i = 0; i < delta; i++)
|
|
|
|
__string[i] = pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __string;
|
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
// Certified 99%
|
1994-08-17 14:07:28 +00:00
|
|
|
char *real ::stringa (int len, int dec, char pad) const
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
string (len, dec, pad);
|
|
|
|
if (dec > 0 || dec == UNDEFINED)
|
|
|
|
eng2ita (__string);
|
|
|
|
return __string;
|
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
// Certified 75%
|
1994-08-17 14:07:28 +00:00
|
|
|
char *real ::literals () const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
const char *primi20[] =
|
|
|
|
{"", "uno", "due", "tre", "quattro",
|
|
|
|
"cinque", "sei", "sette", "otto",
|
|
|
|
"nove", "dieci", "undici", "dodici",
|
1994-08-17 14:07:28 +00:00
|
|
|
"tredici", "quattordici", "quindici", "sedici",
|
|
|
|
"diciassette", "diciotto", "diciannove"};
|
1994-08-17 10:36:55 +00:00
|
|
|
const char *decine[] =
|
|
|
|
{"zero", "dieci", "venti", "trenta", "quaranta",
|
|
|
|
"cinquanta", "sessanta", "settanta", "ottanta",
|
|
|
|
"novanta", "cento"};
|
|
|
|
const char *uni[] =
|
|
|
|
{"uno", "mille", "unmilione", "unmiliardo"};
|
|
|
|
|
|
|
|
const char *potenze[] =
|
|
|
|
{"", "mila", "milioni", "miliardi"};
|
|
|
|
|
|
|
|
__tmp_real = *this;
|
|
|
|
__tmp_real.round (0);
|
1994-10-03 08:47:32 +00:00
|
|
|
TString80 r (__tmp_real.string (0, 0));
|
1994-08-17 10:36:55 +00:00
|
|
|
const bool negativo = r[0] == '-';
|
|
|
|
if (negativo)
|
|
|
|
r.ltrim (1);
|
|
|
|
|
|
|
|
TFixed_string risultato (__string, 80);
|
|
|
|
risultato.cut (0);
|
|
|
|
|
1994-12-02 13:30:45 +00:00
|
|
|
TString80 centinaia;
|
1994-08-17 10:36:55 +00:00
|
|
|
|
|
|
|
for (int migliaia = 0;; migliaia++)
|
|
|
|
{
|
|
|
|
int v = r.len () - 3;
|
|
|
|
if (v < -2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (v < 0)
|
|
|
|
v = 0;
|
|
|
|
const int val = atoi (&r[v]);
|
1994-08-17 14:07:28 +00:00
|
|
|
r.cut (v); // Elimina ultimi 3 caratteri
|
1994-08-17 10:36:55 +00:00
|
|
|
|
|
|
|
v = val;
|
|
|
|
if (v >= 100)
|
|
|
|
{
|
|
|
|
const int c = v / 100;
|
|
|
|
if (c > 1)
|
|
|
|
centinaia = primi20[c];
|
|
|
|
else
|
|
|
|
centinaia.cut (0);
|
|
|
|
v -= c * 100;
|
|
|
|
centinaia << "cento";
|
|
|
|
}
|
|
|
|
const int d = v / 10;
|
|
|
|
if (d > 1)
|
|
|
|
{
|
|
|
|
centinaia << decine[d];
|
|
|
|
v -= d * 10;
|
|
|
|
}
|
|
|
|
if (v != 1)
|
|
|
|
{
|
|
|
|
centinaia << primi20[v] << potenze[migliaia];
|
|
|
|
}
|
|
|
|
else if (val > 1)
|
|
|
|
{
|
|
|
|
if (d > 1)
|
|
|
|
centinaia.cut (centinaia.len () - 1);
|
|
|
|
centinaia << "un" << (migliaia ? potenze[migliaia] : "o");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
centinaia = uni[migliaia];
|
|
|
|
|
|
|
|
risultato.insert (centinaia, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (negativo)
|
|
|
|
risultato.insert ("meno", 0);
|
|
|
|
return __string;
|
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
// Certified 75%
|
1994-08-17 14:07:28 +00:00
|
|
|
char *real ::points (int dec) const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-11-07 13:51:11 +00:00
|
|
|
const char *str = stringa (0, dec);
|
1994-08-17 10:36:55 +00:00
|
|
|
const int neg = (*str == '-') ? 1 : 0;
|
|
|
|
TFixed_string n ((char *) str, 24);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
int dot = n.find (',');
|
|
|
|
if (dot < 0)
|
|
|
|
dot = n.len ();
|
|
|
|
|
|
|
|
if (dec > 0)
|
|
|
|
{
|
|
|
|
if (n[dot] == '\0')
|
|
|
|
n << ',';
|
1994-11-07 13:51:11 +00:00
|
|
|
const int d = strlen (str + dot + 1); // Decimals already there
|
1994-08-17 10:36:55 +00:00
|
|
|
|
|
|
|
if (d <= dec)
|
|
|
|
for (i = d; i < dec; i++)
|
|
|
|
n << '0';
|
|
|
|
else
|
|
|
|
n.cut (dot + dec + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = dot - 3; i > neg; i -= 3)
|
|
|
|
n.insert (".", i);
|
|
|
|
|
|
|
|
return __string;
|
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
|
1994-10-03 08:47:32 +00:00
|
|
|
HIDDEN int get_picture_decimals (const TString& picture)
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
|
|
|
int decimali = 0;
|
|
|
|
const int virgola = picture.find (',');
|
|
|
|
if (virgola >= 0)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
const int len = picture.len ();
|
|
|
|
for (int i = virgola + 1; i < len; i++)
|
|
|
|
if (strchr ("#@~", picture[i]))
|
|
|
|
decimali++;
|
|
|
|
}
|
|
|
|
return decimali;
|
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
|
|
|
|
char *real ::string (const char *picture)
|
|
|
|
const
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
if (*picture == '\0')
|
|
|
|
return string ();
|
|
|
|
if (*picture == '.')
|
|
|
|
return points (atoi (picture + 1));
|
|
|
|
if (strcmp (picture, "LETTERE") == 0)
|
|
|
|
return literals ();
|
|
|
|
|
1994-10-03 08:47:32 +00:00
|
|
|
TString80 v (string());
|
|
|
|
TString80 f (picture);
|
1994-08-17 10:36:55 +00:00
|
|
|
|
|
|
|
const int voluti = get_picture_decimals (f);
|
|
|
|
const int virgola = v.find ('.');
|
|
|
|
int decimali = (virgola >= 0) ? v.len () - virgola - 1 : 0;
|
|
|
|
|
|
|
|
for (; voluti > decimali; decimali++)
|
|
|
|
v << '@';
|
|
|
|
if (voluti < decimali)
|
|
|
|
v.cut (virgola + voluti + (voluti > 0));
|
|
|
|
|
|
|
|
int j = v.len () - 1;
|
|
|
|
for (int i = f.len () - 1; i >= 0 && j >= 0; i--)
|
|
|
|
{
|
|
|
|
char &z = f[i];
|
|
|
|
if (strchr ("#@~", z))
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
char c = v[j--];
|
1994-10-03 08:47:32 +00:00
|
|
|
if (j >= 0 && v[j] == '.')
|
1994-08-17 10:36:55 +00:00
|
|
|
j--;
|
|
|
|
if (z == '~')
|
|
|
|
c = ' ';
|
1995-01-19 14:00:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c == '@')
|
|
|
|
c = (z == '@') ? '0' : ' ';
|
|
|
|
else
|
|
|
|
if (c == '-' && f[i+1] == '.')
|
|
|
|
{
|
|
|
|
f[i+1] = '-';
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
z = c;
|
1994-08-17 09:15:44 +00:00
|
|
|
}
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
|
|
|
for (; i >= 0; i--)
|
|
|
|
switch (f[i])
|
|
|
|
{
|
|
|
|
case '#':
|
|
|
|
case '~':
|
|
|
|
case '.':
|
|
|
|
f[i] = ' ';
|
|
|
|
break;
|
|
|
|
case '@':
|
|
|
|
f[i] = '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return strcpy (__string, f);
|
1994-08-17 09:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
ostream & operator << (ostream & out, const real & a)
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
|
|
|
return out << a.string ();
|
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
|
|
|
|
istream & operator >> (istream & in, real & a)
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
|
|
|
in >> __string;
|
|
|
|
atod (a.ptr (), __string);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
|
|
|
|
int real ::precision ()
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
|
|
|
return dprec (ptr ());
|
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
|
|
|
|
real & real ::round (int prec)
|
1994-08-17 09:15:44 +00:00
|
|
|
{
|
|
|
|
if (prec < 0)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
|
|
|
const double p = ::pow (10.0, -prec);
|
|
|
|
divdfd (ptr (), ptr (), p);
|
|
|
|
dround (ptr (), ptr (), 0);
|
|
|
|
muldfd (ptr (), ptr (), p);
|
|
|
|
}
|
1994-08-17 09:15:44 +00:00
|
|
|
else
|
|
|
|
dround (ptr (), ptr (), prec);
|
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real & real ::ceil (int prec)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
double p = 1.0;
|
1994-08-17 14:07:28 +00:00
|
|
|
if (prec != 0)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
p = ::pow (10.0, -prec);
|
1994-08-17 10:36:55 +00:00
|
|
|
divdfd (ptr (), ptr (), p);
|
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
DEC integer;
|
1994-08-17 10:36:55 +00:00
|
|
|
dint (&integer, ptr ()); // Extract the integer part
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
if (disgt (ptr (), &integer)) // If positive ...
|
1994-12-02 13:30:45 +00:00
|
|
|
addid (ptr (), &integer, 1); // ... add 1
|
1994-08-17 14:07:28 +00:00
|
|
|
else
|
|
|
|
dcpy(ptr(), &integer); // If negative
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
if (prec != 0)
|
1994-08-17 09:15:44 +00:00
|
|
|
muldfd (ptr (), ptr (), p);
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real & real ::trunc (int prec)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dtrunc (ptr (), ptr (), prec);
|
|
|
|
return *this;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator + (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dadd (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator + (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
__tmp_real = a;
|
|
|
|
return __tmp_real += b;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator + (const real & a, double b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
__tmp_real = a;
|
|
|
|
return __tmp_real += b;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator - (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dsub (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator - (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
__tmp_real = a;
|
|
|
|
dsub (__tmp_real.ptr (), __tmp_real.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator - (const real & a, double b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
__tmp_real = b;
|
|
|
|
return __tmp_real -= a;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator *(const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dmul (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator *(double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
muldfd (__tmp_real.ptr (), b.ptr (), a);
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator *(const real & a, double b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
muldfd (__tmp_real.ptr (), a.ptr (), b);
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator / (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
ddiv (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator / (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
__tmp_real = a;
|
|
|
|
ddiv (__tmp_real.ptr (), __tmp_real.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator / (const real & a, double b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
__tmp_real = b;
|
|
|
|
ddiv (__tmp_real.ptr (), a.ptr (), __tmp_real.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator > (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
return disgt (a.ptr (), b.ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator > (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
// dftod(__tmp_real.ptr(), a);
|
1994-08-17 09:15:44 +00:00
|
|
|
// return disgt(__tmp_real.ptr(), b.ptr());
|
|
|
|
const double n = dtodf (b.ptr ());
|
|
|
|
return a > n;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator < (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
return dislt (a.ptr (), b.ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator < (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
// dftod(__tmp_real.ptr(), a);
|
1994-08-17 09:15:44 +00:00
|
|
|
// return dislt(__tmp_real.ptr(), b.ptr());
|
|
|
|
const double n = dtodf (b.ptr ());
|
|
|
|
return a < n;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator >= (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
return disge (a.ptr (), b.ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator >= (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
// dftod(__tmp_real.ptr(), a);
|
1994-08-17 09:15:44 +00:00
|
|
|
// return disge(__tmp_real.ptr(), b.ptr());
|
|
|
|
const double n = dtodf (b.ptr ());
|
|
|
|
return a >= n;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator <= (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
return disle (a.ptr (), b.ptr ());
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator <= (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
// dftod(__tmp_real.ptr(), a);
|
1994-08-17 09:15:44 +00:00
|
|
|
// return disle(__tmp_real.ptr(), b.ptr());
|
|
|
|
const double n = dtodf (b.ptr ());
|
|
|
|
return a <= n;
|
1994-08-12 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator == (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
return diseq (a.ptr (), b.ptr ());
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator == (double a, const real & b)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
const double n = dtodf (b.ptr ());
|
|
|
|
return a == n;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator != (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
return !diseq (a.ptr (), b.ptr ());
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
bool operator != (double a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
const double n = dtodf (b.ptr ());
|
|
|
|
return a != n;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real operator % (const real & a, const long b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dmodl (__tmp_real.ptr (), a.ptr (), b);
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
void swap (real & a, real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
SwapDecimal (a.ptr (), b.ptr ());
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real fnc_min (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dmin (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
1994-08-17 14:07:28 +00:00
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real fnc_max (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dmax (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real sqrt (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dsqrt (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real sqr (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dmul (__tmp_real.ptr (), a.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real exp10 (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dalog (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real pow (const real & a, const real & b)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dpow (__tmp_real.ptr (), a.ptr (), b.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real exp (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dexp (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real log10 (const real & a)
|
1994-08-17 10:36:55 +00:00
|
|
|
|
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dlog (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real log (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dln (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real sin (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dsin (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real cos (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dcos (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real tan (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dtan (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 14:07:28 +00:00
|
|
|
real abs (const real & a)
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
dabs (__tmp_real.ptr (), a.ptr ());
|
|
|
|
return __tmp_real;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
// Distrib
|
|
|
|
// Oggetto per dividere un real in varie sue percentuali
|
|
|
|
// in modo che la loro somma dia sempre il real di partenza
|
1994-08-17 09:15:44 +00:00
|
|
|
// /////////////////////////////////////////////////////////
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-17 09:15:44 +00:00
|
|
|
void TDistrib ::add (real slice)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
if (slice > real (1.0))
|
1994-08-17 10:36:55 +00:00
|
|
|
slice /= 100.0;
|
1994-08-17 09:15:44 +00:00
|
|
|
CHECK (!_ready, "TDistrib: les jeux sont faits");
|
|
|
|
_slices.add (slice);
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-08-25 09:35:18 +00:00
|
|
|
real TDistrib::get ()
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
_ready = TRUE;
|
|
|
|
CHECK (_current < _slices.items (), "TDistrib: too many gets");
|
|
|
|
real r = _tot * ((real &) _slices[_current++]);
|
|
|
|
r.round (_decs);
|
|
|
|
if (r > _tot - _prog)
|
|
|
|
{
|
1994-08-17 10:36:55 +00:00
|
|
|
r = _tot - _prog; _prog = _tot;
|
1994-08-17 09:15:44 +00:00
|
|
|
}
|
|
|
|
else
|
1994-08-17 10:36:55 +00:00
|
|
|
_prog += r;
|
1994-08-17 09:15:44 +00:00
|
|
|
return r;
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|
1994-08-12 10:52:49 +00:00
|
|
|
|
1994-09-09 10:03:59 +00:00
|
|
|
void TDistrib::init (const real & r, bool zap)
|
1994-08-17 10:36:55 +00:00
|
|
|
{
|
1994-08-17 09:15:44 +00:00
|
|
|
_current = 0; _prog = 0;
|
1994-09-09 10:03:59 +00:00
|
|
|
_tot = r; _ready = FALSE;
|
|
|
|
if (zap) _slices.destroy();
|
1994-08-17 10:36:55 +00:00
|
|
|
}
|