1999-06-15 04:50:02 +02:00
|
|
|
/*
|
2004-06-05 01:33:51 +02:00
|
|
|
* Copyright (c) 1999-2004 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
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
2002-08-12 03:34:58 +02:00
|
|
|
#ifdef HAVE_CVS_IDENT
|
2005-08-13 02:45:53 +02:00
|
|
|
#ident "$Id: verireal.cc,v 1.15.2.1 2005/08/13 00:45:55 steve Exp $"
|
1999-06-15 04:50:02 +02:00
|
|
|
#endif
|
|
|
|
|
|
2001-07-25 05:10:48 +02:00
|
|
|
# include "config.h"
|
|
|
|
|
|
1999-06-15 04:50:02 +02:00
|
|
|
# include "verireal.h"
|
2003-02-07 03:47:57 +01:00
|
|
|
# include "verinum.h"
|
2003-03-07 07:10:13 +01:00
|
|
|
# include <stdlib.h>
|
1999-06-15 04:50:02 +02:00
|
|
|
# include <ctype.h>
|
2000-12-10 23:01:35 +01:00
|
|
|
# include <iostream>
|
2003-01-26 22:15:58 +01:00
|
|
|
# include <math.h>
|
1999-06-15 04:50:02 +02:00
|
|
|
# include <assert.h>
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
verireal::~verireal()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-10 23:01:35 +01:00
|
|
|
long verireal::as_long(int shift) const
|
|
|
|
|
{
|
2003-02-07 07:13:44 +01:00
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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 verinum&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
2003-02-07 07:13:44 +01:00
|
|
|
res.value_ = l.value_ / (double)r.as_long();
|
2003-02-07 03:47:57 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verireal operator% (const verireal&l, const verireal&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
|
|
|
|
assert(0);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verireal operator% (const verireal&l, const verinum&r)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
|
|
|
|
assert(0);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-05 01:33:51 +02:00
|
|
|
verireal operator- (const verireal&l)
|
|
|
|
|
{
|
|
|
|
|
verireal res;
|
|
|
|
|
res.value_ = - l.value_;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-13 02:45:53 +02:00
|
|
|
std::ostream& operator<< (std::ostream&out, const verireal&v)
|
2000-12-10 23:01:35 +01:00
|
|
|
{
|
2003-02-07 07:13:44 +01:00
|
|
|
out << v.value_;
|
2001-07-07 22:20:10 +02:00
|
|
|
return out;
|
2000-12-10 23:01:35 +01:00
|
|
|
}
|
|
|
|
|
|
1999-06-15 04:50:02 +02:00
|
|
|
/*
|
|
|
|
|
* $Log: verireal.cc,v $
|
2005-08-13 02:45:53 +02:00
|
|
|
* Revision 1.15.2.1 2005/08/13 00:45:55 steve
|
|
|
|
|
* Fix compilation warnings/errors with newer compilers.
|
|
|
|
|
*
|
2004-06-05 01:33:51 +02:00
|
|
|
* Revision 1.15 2004/06/04 23:33:51 steve
|
|
|
|
|
* Add unary minus as operator supported by verireal.
|
|
|
|
|
*
|
2003-03-07 07:10:13 +01:00
|
|
|
* Revision 1.14 2003/03/07 06:10:13 steve
|
|
|
|
|
* Use strtod to convert text to doubles.
|
|
|
|
|
*
|
2003-03-05 04:45:01 +01:00
|
|
|
* Revision 1.13 2003/03/05 03:45:01 steve
|
|
|
|
|
* Restore verireal constructor to match vvp processing of reals.
|
2003-03-05 03:36:05 +01:00
|
|
|
*
|
2003-02-07 07:13:44 +01:00
|
|
|
* Revision 1.11 2003/02/07 06:13:44 steve
|
|
|
|
|
* Store real values as native double.
|
|
|
|
|
*
|
2003-02-07 03:47:57 +01:00
|
|
|
* Revision 1.10 2003/02/07 02:48:43 steve
|
|
|
|
|
* NetEBDiv handles real value constant expressions.
|
|
|
|
|
*
|
2003-01-26 22:15:58 +01:00
|
|
|
* Revision 1.9 2003/01/26 21:15:59 steve
|
|
|
|
|
* Rework expression parsing and elaboration to
|
|
|
|
|
* accommodate real/realtime values and expressions.
|
|
|
|
|
*
|
2002-08-12 03:34:58 +02:00
|
|
|
* Revision 1.8 2002/08/12 01:35:01 steve
|
|
|
|
|
* conditional ident string using autoconfig.
|
|
|
|
|
*
|
2002-06-15 04:35:49 +02:00
|
|
|
* Revision 1.7 2002/06/15 02:35:49 steve
|
|
|
|
|
* Rounding error.
|
|
|
|
|
*
|
2001-11-06 07:11:55 +01:00
|
|
|
* Revision 1.6 2001/11/06 06:11:55 steve
|
|
|
|
|
* Support more real arithmetic in delay constants.
|
|
|
|
|
*
|
2001-07-25 05:10:48 +02:00
|
|
|
* Revision 1.5 2001/07/25 03:10:50 steve
|
|
|
|
|
* Create a config.h.in file to hold all the config
|
|
|
|
|
* junk, and support gcc 3.0. (Stephan Boettcher)
|
|
|
|
|
*
|
2001-07-07 22:20:10 +02:00
|
|
|
* Revision 1.4 2001/07/07 20:20:10 steve
|
|
|
|
|
* Pass parameters to system functions.
|
|
|
|
|
*
|
2000-12-10 23:01:35 +01:00
|
|
|
* Revision 1.3 2000/12/10 22:01:36 steve
|
|
|
|
|
* Support decimal constants in behavioral delays.
|
|
|
|
|
*
|
2000-02-23 03:56:53 +01:00
|
|
|
* Revision 1.2 2000/02/23 02:56:56 steve
|
|
|
|
|
* Macintosh compilers do not support ident.
|
|
|
|
|
*
|
1999-06-15 04:50:02 +02:00
|
|
|
* Revision 1.1 1999/06/15 02:50:02 steve
|
|
|
|
|
* Add lexical support for real numbers.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|