Use sscanf to make doubles from strings.

This commit is contained in:
steve 2003-03-05 02:36:05 +00:00
parent 59f820aacd
commit 1e73cf82a6
1 changed files with 17 additions and 48 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999 Stephen Williams (steve@icarus.com) * Copyright (c) 1999-2003 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
#ifdef HAVE_CVS_IDENT #ifdef HAVE_CVS_IDENT
#ident "$Id: verireal.cc,v 1.11 2003/02/07 06:13:44 steve Exp $" #ident "$Id: verireal.cc,v 1.12 2003/03/05 02:36:05 steve Exp $"
#endif #endif
# include "config.h" # include "config.h"
@ -28,6 +28,7 @@
# include <iostream> # include <iostream>
# include <math.h> # include <math.h>
# include <assert.h> # include <assert.h>
# include <stdio.h>
verireal::verireal() verireal::verireal()
{ {
@ -36,58 +37,23 @@ verireal::verireal()
verireal::verireal(const char*txt) verireal::verireal(const char*txt)
{ {
unsigned long mant = 0; char*buf = new char[strlen(txt)+1];
bool sign = false; char*cp = buf;
signed int exp10 = 0;
const char*ptr = txt; /* filter out Verilog-isms */
for ( ; *ptr ; ptr += 1) { for (unsigned i = 0; txt[i]; i += 1) {
if (*ptr == '.') break; if (txt[i] == '_') { continue; }
if (*ptr == 'e') break; *cp = txt[i]; cp++;
if (*ptr == 'E') break;
if (*ptr == '_') continue;
assert(isdigit(*ptr));
mant *= 10;
mant += *ptr - '0';
} }
if (*ptr == '.') { *cp = '\0';
ptr += 1;
for ( ; *ptr ; ptr += 1) {
if (*ptr == 'e') break;
if (*ptr == 'E') break;
if (*ptr == '_') continue;
assert(isdigit(*ptr)); if (sscanf(buf, "%lf", &value_) != 1) {
fprintf(stderr, "PANIC: Unable to sscanf(%s)\n", txt);
mant *= 10; assert(0);
mant += *ptr - '0';
exp10 -= 1;
}
} }
if ((*ptr == 'e') || (*ptr == 'E')) { delete[]buf;
ptr += 1;
long tmp = 0;
bool sflag = false;
if (*ptr == '+') {ptr += 1; sflag = false;}
if (*ptr == '-') {ptr += 1; sflag = true;}
for ( ; *ptr ; ptr += 1) {
if (*ptr == '_') continue;
assert(isdigit(*ptr));
tmp *= 10;
tmp += *ptr - '0';
}
exp10 += sflag? -tmp : +tmp;
}
assert(*ptr == 0);
value_ = pow(10.0,exp10) * mant * (sign? -1.0 : 1.0);
} }
verireal::verireal(long val) verireal::verireal(long val)
@ -164,6 +130,9 @@ ostream& operator<< (ostream&out, const verireal&v)
/* /*
* $Log: verireal.cc,v $ * $Log: verireal.cc,v $
* Revision 1.12 2003/03/05 02:36:05 steve
* Use sscanf to make doubles from strings.
*
* Revision 1.11 2003/02/07 06:13:44 steve * Revision 1.11 2003/02/07 06:13:44 steve
* Store real values as native double. * Store real values as native double.
* *