Skip to content

Commit

Permalink
Implement double_to_scientific()
Browse files Browse the repository at this point in the history
And use it for ast.h and asr.h
  • Loading branch information
certik committed Jun 10, 2022
1 parent 7b2e083 commit 60cd916
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions grammar/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def visitField(self, field, cons, last):
self.emit('s.append(std::to_string(x.m_%s));' % field.name, 2)
elif field.type == "float" and not field.seq and not field.opt:
self.emit('s.append("\\n" + indtd + "%s" + "%s=");' % (arr, field.name), 2)
self.emit('s.append(std::to_string(x.m_%s));' % field.name, 2)
self.emit('s.append(double_to_scientific(x.m_%s));' % field.name, 2)
elif field.type == "bool" and not field.seq and not field.opt:
self.emit('s.append("\\n" + indtd + "%s" + "%s=");' % (arr, field.name), 2)
self.emit("if (x.m_%s) {" % field.name, 2)
Expand Down Expand Up @@ -1178,7 +1178,7 @@ def visitField(self, field, cons):
else:
self.emit('s.append(std::to_string(x.m_%s));' % field.name, 2)
elif field.type == "float" and not field.seq and not field.opt:
self.emit('s.append(std::to_string(x.m_%s));' % field.name, 2)
self.emit('s.append(double_to_scientific(x.m_%s));' % field.name, 2)
elif field.type == "bool" and not field.seq and not field.opt:
self.emit("if (x.m_%s) {" % field.name, 2)
self.emit( 's.append(".true.");', 3)
Expand Down
6 changes: 6 additions & 0 deletions src/libasr/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ struct Str {
static_assert(std::is_standard_layout<Str>::value);
static_assert(std::is_trivial<Str>::value);

static inline std::string double_to_scientific(double x) {
std::string buf(30, '\0');
std::sprintf(&buf[0], "%e", x);
return buf;
}

} // namespace LFortran


Expand Down

0 comments on commit 60cd916

Please sign in to comment.