Operation in String to Double conversion

W

William Lopes

Hi guys!

So, I have to do a conversion between String and Double object, but my string is a mathematical operation like "100 + 10". Even when I make a conversion using NumberFormat.getInstance of "100 + 10", my result is 100.0 only.

I would like to do it without split my string of way manually.

Someone could help me?

Thanks!
 
W

William Lopes

Em domingo, 14 de outubro de 2012 17h31min11s UTC-3, Martin Gregorie escreveu:
Sounds like you need an mathematical expression parser. I don't think

there's anything suitable in the standard classes. You can:



1)see if there's a third party mathematical expression parser available



2)use a compiler generator like Coco/R

http://www.ssw.uni-linz.ac.at/Research/Projects/Coco/

to create one.



3)attempt to roll your own from scratch.



If it was my problem I'd work down that list in the order shown unless

there are constraints set, i.e. its an assignment that you are required

to solve by yourself.



I've used Coco/R to generate an equivalent Java class that could handle

the sort of expressions used in C preprocessor commands: it was dead easy

once I understood how Coco/R works, but then again I had previously

solved non-trivial problems with lex and yacc.





--

martin@ | Martin Gregorie

gregorie. | Essex, UK

org |

Thank you, using the your keywords I achieved to find a that can help me.

See: http://www.objecthunter.net/tinybo/blog/articles/86

Ps.: I didn't still tested it.

Hugs.
 
M

markspace

Thank you, using the your keywords I achieved to find a that can help me.

See: http://www.objecthunter.net/tinybo/blog/articles/86

Ps.: I didn't still tested it.


I don't think I much care for that site you link too. Too many spelling
mistakes ("i" and "java") for me to take it seriously.

Parsers aren't hard to write, and often they're used as example in
introductory texts. The C++ Programming Language by Bjarne Stroustrup,
for example, has a complete parser early in the text. Likewise Learning
Java published by O'Reilly whips up a quick algebraic parser for a demo
spreadsheet program.

Is this for a class or something? Usually if you need to evaluate
expressions there's a library that does that for you. It's really kind
of rare to need to do your own.
 
W

William Lopes

Em domingo, 14 de outubro de 2012 19h59min26s UTC-3, markspace escreveu:
I don't think I much care for that site you link too. Too many spelling

mistakes ("i" and "java") for me to take it seriously.



Parsers aren't hard to write, and often they're used as example in

introductory texts. The C++ Programming Language by Bjarne Stroustrup,

for example, has a complete parser early in the text. Likewise Learning

Java published by O'Reilly whips up a quick algebraic parser for a demo

spreadsheet program.



Is this for a class or something? Usually if you need to evaluate

expressions there's a library that does that for you. It's really kind

of rare to need to do your own.

In true the page is it http://www.objecthunter.net/exp4j/index.html
 
R

Roedy Green

So, I have to do a conversion between String and Double object,
but my string is a mathematical operation like "100 + 10". Even when I
make a conversion using NumberFormat.getInstance of "100 + 10", my
result is 100.0 only.
I would like to do it without split my string of way manually.

Someone could help me?

There is no "eval" function to parse and expressions and perform
arithmetic at compile time. To do that you need a parser or an
interpretive language like JPython.

for general conversion help see
http://mindprod.com/applet/converter.html

You can take that expression apart, with a regex if they are not too
complicated.
see http://mindprod.com/jgloss/regex.html
 
J

John B. Matthews

William Lopes said:
I have to do a conversion between String and Double object, but my
string is a mathematical operation like "100 + 10". Even when I make
a conversion using NumberFormat.getInstance of "100 + 10", my result
is 100.0 only.

I would like to do it without split my string of way manually.

Your distribution may already contain a suitable implementation of
javax.script.ScriptEngine:

ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory f : factories) {
System.out.println(f);
}

Selecting the available RhinoScriptEngine by extension

ScriptEngine engine = mgr.getEngineByExtension("js");
try {
System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
ex.printStackTrace(System.err);
}

prints the expected answer, 42.0.
 
F

frank.asseg

I don't think I much care for that site you link too. Too many spelling
mistakes ("i" and "java") for me to take it seriously.
wow, sorry for not complying with your orthographical requirements, although imho that's hardly grounds to disregard the library.
maybe this site is more to your liking:
http://www.objecthunter.net/exp4j/
although there are probably some typos in there too ;)
the lib may not be perfect, and i'm sure there are things that can be doneto optimize the implementation, changes i'd be happy to adapt. so take a look at the github project and drop me a line if you have some constructive criticism regarding the implementation.
 
F

frank.asseg

I don't think I much care for that site you link too. Too many spelling
mistakes ("i" and "java") for me to take it seriously.

wow, sorry for not complying with your orthographical requirements, although imho that's hardly grounds to disregard the library.
maybe this site is more to your liking:
http://www.objecthunter.net/exp4j/
although there are probably some typos in there too ;)
the lib may not be perfect, and i'm sure there are things that can be doneto optimize the implementation, changes i'd be happy to adapt. so take a look at the github project and drop me a line if you have some constructive criticism regarding the implementation.
 
L

Lew

(e-mail address removed) wrote:

You forgot to attribute your citation.
wow, sorry for not complying with your orthographical requirements, although imho that's hardly
grounds to disregard the library.

On the contrary, it's a really reliable bellwether of product quality.

Those who are careless when you're dating are unlikely to be courteous when you're married.

If you cannot even promote a product with professionalism and attention to detail, it is very
unlikely the product will reflect a higher standard.
maybe this site is more to your liking:

http://www.objecthunter.net/exp4j/

although there are probably some typos in there too ;)

the lib may not be perfect, and i'm [sic] sure there are things that can be done to optimize the
implementation, changes i'd [sic] be happy to adapt. so take a look at the github project and drop me
a line if you have some constructive criticism regarding the implementation.

He already gave you constructive criticism and you acted like a jerk about it. Why would anyone waste
any more time on it?
 
D

Daniel Pitts

Your distribution may already contain a suitable implementation of
javax.script.ScriptEngine:

ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory f : factories) {
System.out.println(f);
}

Selecting the available RhinoScriptEngine by extension

ScriptEngine engine = mgr.getEngineByExtension("js");
try {
System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
ex.printStackTrace(System.err);
}

prints the expected answer, 42.0.
Another library I tend to favor is OGNL, it will solve your problem
specifically, and is much more powerful. It *is* a programming language
in its own rite, so use with caution (eg, only strings you from trusted
sources).
 
F

frank.asseg

If you cannot even promote a product with professionalism and attention to
detail, it is very unlikely the product will reflect a higher standard.
again i beg to differ. the code quality has got nothing to do with the manner an article about the library is written. i think judging a developer's skill based on his professionalism as an editor is like saying a monkey can't climb because he sucks at flying.
He already gave you constructive criticism and you acted like a jerk about it.
well i certainly didn't call him a jerk for having a different opinion. so in order not to succumb to godwin's law i won't respond to any more posts regarding the manner in which i choose to write.
 
L

Lew

Lew said:
If you cannot even promote a product with professionalism and attention to
detail, it is very unlikely the product will reflect a higher standard.

again i [sic] beg to differ. the code quality has got nothing to do with the manner an article about the
library is written. i think judging a developer's skill based on his professionalism as an editor is like
saying a monkey can't climb because he sucks at flying.

You can make up all the cute similes you like, but my experience is solid in this area.

Illiterate descriptions rarely correspond to quality products.
He already gave you constructive criticism and you acted like a jerk about it.

well i [sic] certainly didn't call him a jerk for having a different opinion.
so in order not to succumb to godwin's law i [sic] won't respond to any more posts regarding the
manner in which i [sic] choose to write.

Good luck with that attitude. You'll need it.

And you not calling him a jerk didn't mean you didn't act like one. Your whole attitude is
resentful and hostile despite being given good advice.

No one but you cares about your ego.
 
A

Arved Sandstrom

again i beg to differ. the code quality has got nothing to do with the manner an article about the library is written. i think judging a developer's skill based on his professionalism as an editor is like saying a monkey can't climb because he sucks at flying.

well i certainly didn't call him a jerk for having a different opinion. so in order not to succumb to godwin's law i won't respond to any more posts regarding the manner in which i choose to write.
The way I see it, the project page is fine. The article does have
grammar and spelling problems to the extent that I also, like Lew,
started to question the code...*until* I noticed that the author
(yourself) is from SW Germany.

This is one of the first things I do when I see that software
documentation and/or related writings have grammar and spelling problems
- I check to see if English is the author's first language.

AHS
 
D

Daniele Futtorovic

wow, sorry for not complying with your orthographical requirements, although imho that's hardly grounds to disregard the library.
maybe this site is more to your liking:
http://www.objecthunter.net/exp4j/
although there are probably some typos in there too ;)
the lib may not be perfect, and i'm sure there are things that can be done to optimize the implementation, changes i'd be happy to adapt. so take a look at the github project and drop me a line if you have some constructive criticism regarding the implementation.

You are the author that lib, right? Then allow me, for what it is worth,
to opine with markspace -- not only, incidentally, with his opinion, but
more importantly and fundamentally, with his approach.

/Le style, c'est l'homme/, as the saying goes. Style matters. Not so
much in and of itself, but to the extent of what it tells you about the
author. It doesn't tell you everything, and it is not always right, but
it is so more often than not. And most importantly, it allows you to
come to an at least preliminary conclusion in the face of a limited set
of data.

Being able to make quick decisions is a crucial skill, as I am sure
you'll realise, or would after giving it minimal thought. You need means
to separate the wheat from the chaff -- the quicker you are able to do
this, the more productive you can be. When choosing a library, you
rarely have the luxury of analysing it in every last detail. Especially
if it performs a fairly common, and merely cumbersome, task, picking it
apart might take longer than just writing the damn thing yourself.

In such a situation, you need to discriminate effectively; you need
effective discriminators. Good-will and trust -- say, if it's a library
published by someone whose other works you've used to your satisfaction
-- can be one such discriminator, and perhaps the most important one.
Failing that, or in conjunction with that, apparent style (code style,
but also, as they're related, lexical and overall style) is perhaps the
second most important one.

That being said, your grammar and, if you'll pardon my saying so, the
slight snottiness you display here notwithstanding, the code examples on
your page look fairly okay, and if I were the OP, and didn't have the
alternative of using the scripting engine (or if my task required
functions and the other more advanced features you offer), I'd probably
give your lib a good second look.

My two cents. Please don't flame me.
 
F

frank.asseg

[...] and if I were the OP, and didn't have the
alternative of using the scripting engine (or if my task required
functions and the other more advanced features you offer), I'd probably
give your lib a good second look.
the jsr223 scripting engine works fine, but is rather slow, this is the comparison with pure java math, livetribe's jsr223 implementation and exp4j from the tests on my local desktop machine:

expression log(x) - y * (sqrt(x^cos(y)))
exp4j 1123189 [561.59k calc/sec]
Java Math 4590231 [2295.12k calc/sec]
JSR 223(Javascript) 1674 [837.0 calc/sec]

This of course is a very synthetic benchmark, but be aware that the scripting engine can be quite slow when performing a lot of operations, and a lot of performance is to be gained with a pure java implementation. and this argument does neither include the time to warm up the script engine nor the increased memory footprint.
 
E

Eric Sosman

Hi guys!

So, I have to do a conversion between String and Double object, but my string is a mathematical operation like "100 + 10". Even when I make a conversion using NumberFormat.getInstance of "100 + 10", my result is 100.0 only.

I would like to do it without split my string of way manually.

I wrote an expression evaluator will accept such a string and
produce an object instance that can then be evaluated:

Expression e = Expression.compile("100 + 10", null);
double value = e.value(null);

More generally,

// Formula for volume of a cylinder:
Expression e = Expression.compile(
"(diameter/2)^2 * PI * height",
new String[] { "diameter", "height" });

// Volume of a cylinder of diameter 4, height 10:
double volume = e.value(new double[] { 4.0, 10.0 });

The only unusual feature is that expressions compile directly to
Java byte code rather than to an interpreted pseudo-code; their
value() methods can therefore be JITted for speedier execution.
This is surely overkill for your problem (and for many others;
it was an over-engineered learning exercise), but should solve it
nonetheless. Internally, though, the compiler splits the string
"of my way manually," which you seem to find distasteful. Sue me.

Send an E-mail address if you'd like a copy. No warranties.
 
J

John B. Matthews

Daniel Pitts said:
Another library I tend to favor is OGNL, it will solve your problem
specifically, and is much more powerful.

Cited for a tantalizing suggestion regarding TableModel and an important
tip on pronunciation.

It *is* a programming language in its own rite, so use with caution
(eg, only strings you from trusted sources).

This bears repeating; it also applies to the RhinoScriptEngine, et al.
 
A

Arne Vajhoej

So, I have to do a conversion between String and Double object, but
my string is a mathematical operation like "100 + 10". Even when I
make a conversion using NumberFormat.getInstance of "100 + 10", my
result is 100.0 only.

I would like to do it without split my string of way manually.

Many options:
* Write an expression evaluator from scratch
* Write an expression evaluator using a parser generator
* Generate Java code, compile it, load it and call it
* Call a script engine (JavaScript, BeanShell etc.)

Unless performance is critical I would go for BeanShell. Any
Java developer should know the syntax and it is relative easy
to call.

Arne
 
A

Arne Vajhoej

The way I see it, the project page is fine. The article does have
grammar and spelling problems to the extent that I also, like Lew,
started to question the code...*until* I noticed that the author
(yourself) is from SW Germany.

This is one of the first things I do when I see that software
documentation and/or related writings have grammar and spelling problems
- I check to see if English is the author's first language.

In general I agree.

But "java" does not look good in German either.

Arne
 
A

Arne Vajhoej

well i certainly didn't call him a jerk for having a different
opinion.

You got criticism and you replied with sarcastic comments.

So you demonstrated that you are not willing to listen to
criticism and attack those that provide such.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top