1999-06-15 04:50:02 +02:00
|
|
|
/*
|
2021-11-04 17:12:04 +01:00
|
|
|
* Copyright (c) 1999-2021 Stephen Williams (steve@icarus.com)
|
1999-06-15 04:50:02 +02:00
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form under the terms of the GNU
|
|
|
|
|
* General Public License as published by the Free Software
|
|
|
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1999-06-15 04:50:02 +02:00
|
|
|
*/
|
|
|
|
|
|
2001-07-25 05:10:48 +02:00
|
|
|
# include "config.h"
|
2009-01-09 18:43:02 +01:00
|
|
|
# include "compiler.h"
|
2001-07-25 05:10:48 +02:00
|
|
|
|
1999-06-15 04:50:02 +02:00
|
|
|
# include "verireal.h"
|
2003-02-07 03:47:57 +01:00
|
|
|
# include "verinum.h"
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cstdlib>
|
|
|
|
|
# include <cctype>
|
2000-12-10 23:01:35 +01:00
|
|
|
# include <iostream>
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cmath>
|
|
|
|
|
# include <cassert>
|
2008-01-05 00:23:47 +01:00
|
|
|
# include <cstring>
|
1999-06-15 04:50:02 +02:00
|
|
|
|
2021-11-04 17:12:04 +01:00
|
|
|
using namespace std;
|
|
|
|
|
|
1999-06-15 04:50:02 +02:00
|
|
|
verireal::verireal()
|
|
|
|
|
{
|
2003-02-07 07:13:44 +01:00
|
|
|
value_ = 0.0;
|
1999-06-15 04:50:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verireal::verireal(const char*txt)
|
|
|
|
|
{
|
2003-03-07 07:10:13 +01:00
|
|
|
char*tmp = new char[strlen(txt)+1];
|
|
|
|
|
char*cp = tmp;
|
|
|
|
|
for (unsigned idx = 0 ; txt[idx] ; idx += 1) {
|
|
|
|
|
if (txt[idx] == '_')
|
|
|
|
|
continue;
|
1999-06-15 04:50:02 +02:00
|
|
|
|
2003-03-07 07:10:13 +01:00
|
|
|
*cp++ = txt[idx];
|
2003-03-05 04:45:01 +01:00
|
|
|
}
|
2003-03-07 07:10:13 +01:00
|
|
|
cp[0] = 0;
|
2003-03-05 04:45:01 +01:00
|
|
|
|
2003-03-07 07:10:13 +01:00
|
|
|
value_ = strtod(tmp, 0);
|
|
|
|
|
delete[]tmp;
|
1999-06-15 04:50:02 +02:00
|
|
|
}
|
|
|
|
|
|
2001-11-06 07:11:55 +01:00
|
|
|
verireal::verireal(long val)
|
|
|
|
|
{
|
2003-02-07 07:13:44 +01:00
|
|
|
value_ = (double)val;
|
2001-11-06 07:11:55 +01:00
|
|
|
}
|
1999-06-15 04:50:02 +02:00
|
|
|
|
2006-10-03 07:06:00 +02:00
|
|
|
verireal::verireal(double val)
|
|
|
|
|
{
|
|
|
|
|
value_ = val;
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-15 04:50:02 +02:00
|
|
|
verireal::~verireal()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 05:12:51 +01:00
|
|
|
long verireal::as_long() const
|
2000-12-10 23:01:35 +01:00
|
|
|
{
|
2022-01-18 05:12:51 +01:00
|
|
|
double out = value_;
|
2003-02-07 07:13:44 +01:00
|
|
|
double outf;
|
|
|
|
|
|
|
|
|
|
if (out >= 0.0) {
|
|
|
|
|
outf = floor(out);
|
|
|
|
|
if (out >= (outf + 0.5))
|
|
|
|
|
outf += 1.0;
|
|
|
|
|
} else {
|
|
|
|
|
outf = ceil(out);
|
|
|
|
|
if (out <= (outf - 0.5))
|
|
|
|
|
outf -= 1.0;
|
2000-12-10 23:01:35 +01:00
|
|
|
}
|
2003-02-07 07:13:44 +01:00
|
|
|
return (long) outf;
|
2000-12-10 23:01:35 +01:00
|
|
|
}
|
|
|
|
|
|
2006-08-08 07:11:37 +02:00
|
|
|
int64_t verireal::as_long64(int shift) const
|
|
|
|
|
{
|
|
|
|
|
double out = value_ * pow(10.0,shift);
|
|
|
|
|
double outf;
|
|
|
|
|
|
|
|
|
|
if (out >= 0.0) {
|
|
|
|
|
outf = floor(out);
|
|
|
|
|
if (out >= (outf + 0.5))
|
|
|
|
|
outf += 1.0;
|
|
|
|
|
} else {
|
|
|
|
|
outf = ceil(out);
|
|
|
|
|
if (out <= (outf - 0.5))
|
|
|
|
|
outf -= 1.0;
|
|
|
|
|
}
|
|
|
|
|
return (int64_t) outf;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-26 22:15:58 +01:00
|
|
|
double verireal::as_double() const
|
|
|
|
|
{
|
2003-02-07 07:13:44 +01:00
|
|
|
return value_;
|
2003-01-26 22:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
2007-04-07 06:46:18 +02:00
|
|
|
verireal operator+ (const verireal&l, const verireal&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
|
|
|
|
res.value_ = l.value_ + r.value_;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verireal operator- (const verireal&l, const verireal&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
|
|
|
|
res.value_ = l.value_ - r.value_;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-06 07:11:55 +01:00
|
|
|
verireal operator* (const verireal&l, const verireal&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
2003-02-07 07:13:44 +01:00
|
|
|
res.value_ = l.value_ * r.value_;
|
2001-11-06 07:11:55 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-07 03:47:57 +01:00
|
|
|
verireal operator/ (const verireal&l, const verireal&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
2003-02-07 07:13:44 +01:00
|
|
|
res.value_ = l.value_ / r.value_;
|
2003-02-07 03:47:57 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verireal operator% (const verireal&l, const verireal&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
2009-01-09 18:43:02 +01:00
|
|
|
// Modulus of a real value is not supported by the standard,
|
|
|
|
|
// but we support it as an extension. Assert that we are in
|
|
|
|
|
// the correct state before doing the operation.
|
|
|
|
|
assert(gn_icarus_misc_flag);
|
|
|
|
|
res.value_ = fmod(l.value_, r.value_);
|
2003-02-07 03:47:57 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-05 01:33:51 +02:00
|
|
|
verireal operator- (const verireal&l)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
|
|
|
|
res.value_ = - l.value_;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-10 23:01:35 +01:00
|
|
|
ostream& operator<< (ostream&out, const verireal&v)
|
|
|
|
|
{
|
2009-04-02 21:03:31 +02:00
|
|
|
out << showpoint << v.value_;
|
2001-07-07 22:20:10 +02:00
|
|
|
return out;
|
2000-12-10 23:01:35 +01:00
|
|
|
}
|