Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #23671 > unrolled thread

Expresson evaluation in javascript

Started byjonas.thornvall@gmail.com
First post2014-03-25 11:24 -0700
Last post2014-03-30 05:01 -0400
Articles 20 on this page of 25 — 10 participants

Back to article view | Back to comp.lang.javascript


Contents

  Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-25 11:24 -0700
    Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-25 11:43 -0700
      Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-25 11:44 -0700
    Re: Expresson evaluation in javascript Danny <dann90038@gmail.com> - 2014-03-25 14:04 -0700
    Re: Expresson evaluation in javascript Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-03-25 18:40 -0300
    Re: Expresson evaluation in javascript Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-25 23:49 +0100
      Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-25 22:43 -0700
        Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-25 23:29 -0700
          Re: Expresson evaluation in javascript Jim T. <x@y.z> - 2014-03-28 11:48 -0500
            Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-28 10:03 -0700
              Re: Expresson evaluation in javascript Jim T. <x@y.z> - 2014-03-28 13:12 -0500
              Re: Expresson evaluation in javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-28 18:33 +0100
                Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-28 10:50 -0700
                  Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-28 10:59 -0700
                    Re: Expresson evaluation in javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-28 20:38 +0100
                    Re: Expresson evaluation in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-03-29 19:49 -0700
                  Re: Expresson evaluation in javascript Ed Jay <edMbj@aes-intl.com> - 2014-03-29 10:53 -0700
        Re: Expresson evaluation in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-03-29 19:45 -0700
          Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-30 09:21 -0700
          Re: Expresson evaluation in javascript jonas.thornvall@gmail.com - 2014-03-30 09:24 -0700
            Re: Expresson evaluation in javascript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-03-30 21:19 +0200
            Re: Expresson evaluation in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-03-30 14:20 -0700
    Re: Expresson evaluation in javascript John Harris <niam@jghnorth.org.uk.invalid> - 2014-03-26 14:58 +0000
    Re: Expresson evaluation in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-03-29 19:33 -0700
    Re: Expresson evaluation in javascript Norman Peelman <npeelman@cfl.rr.com> - 2014-03-30 05:01 -0400

Page 1 of 2  [1] 2  Next page →


#23671 — Expresson evaluation in javascript

Fromjonas.thornvall@gmail.com
Date2014-03-25 11:24 -0700
SubjectExpresson evaluation in javascript
Message-ID<62248b63-802b-4e9c-b3d3-d31c5003a24d@googlegroups.com>
In python i beleive mathematical expression can be directly evaluated, if in correct form. 

Javascript do not have eval?

If not, how do one go ahead evaluate something with two constant operands but where the operator is a variable? 

z=*;   x=3z5

z=/    x=3z3

z=+    x=3z3

z=-    x=3z3

 
Will there be a eval function in future javascript?

[toc] | [next] | [standalone]


#23674

Fromjonas.thornvall@gmail.com
Date2014-03-25 11:43 -0700
Message-ID<b342062d-0a05-46af-bc2d-0d0aebc7c365@googlegroups.com>
In reply to#23671
Den tisdagen den 25:e mars 2014 kl. 19:24:41 UTC+1 skrev jonas.t...@gmail.com:
> In python i beleive mathematical expression can be directly evaluated, if in correct form. 
> 
> 
> 
> Javascript do not have eval?
> 
> 
> 
> If not, how do one go ahead evaluate something with two constant operands but where the operator is a variable? 
> 
> 
> 
> z=*;   x=3z5
> 
> 
> 
> z=/    x=3z3
> 
> 
> 
> z=+    x=3z3
> 
> 
> 
> z=-    x=3z3
> 
> 
> 
>  
> 
> Will there be a eval function in future javascript?

It seem a bit tedious to loop throug the string with charAt() putting the operands in an array? And then use str.split to get the operands?

And also the order of evaluation must be correct?

Anyone have another solution?

[toc] | [prev] | [next] | [standalone]


#23675

Fromjonas.thornvall@gmail.com
Date2014-03-25 11:44 -0700
Message-ID<841dca16-a08a-4ba3-ab2b-e37322c64118@googlegroups.com>
In reply to#23674
Den tisdagen den 25:e mars 2014 kl. 19:43:09 UTC+1 skrev jonas.t...@gmail.com:
> Den tisdagen den 25:e mars 2014 kl. 19:24:41 UTC+1 skrev jonas.t...@gmail.com:
> 
> > In python i beleive mathematical expression can be directly evaluated, if in correct form. 
> 
> > 
> 
> > 
> 
> > 
> 
> > Javascript do not have eval?
> 
> > 
> 
> > 
> 
> > 
> 
> > If not, how do one go ahead evaluate something with two constant operands but where the operator is a variable? 
> 
> > 
> 
> > 
> 
> > 
> 
> > z=*;   x=3z5
> 
> > 
> 
> > 
> 
> > 
> 
> > z=/    x=3z3
> 
> > 
> 
> > 
> 
> > 
> 
> > z=+    x=3z3
> 
> > 
> 
> > 
> 
> > 
> 
> > z=-    x=3z3
> 
> > 
> 
> > 
> 
> > 
> 
> >  
> 
> > 
> 
> > Will there be a eval function in future javascript?
> 
> 
> 
> It seem a bit tedious to loop throug the string with charAt() putting the operands in an array? And then use str.split to get the operands?
> 
> 
> 
> And also the order of evaluation must be correct?
> 
> 
> 
> Anyone have another solution?

charAt() putting the ***operators ***in an array?

[toc] | [prev] | [next] | [standalone]


#23677

FromDanny <dann90038@gmail.com>
Date2014-03-25 14:04 -0700
Message-ID<d9f9d146-d0bc-4972-a53c-db644135e021@googlegroups.com>
In reply to#23671
To Jonas

you'd usually send the arguments with the numbers and perform
the operation on a subroutine or module
rather than what you're suggesting

there's an eval() in js, views about is a bit divided
though is very useful for say parsing json metadata
but it'd not evaluate what you have, and IMO rightfully so

[toc] | [prev] | [next] | [standalone]


#23678

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2014-03-25 18:40 -0300
Message-ID<lgst3k$hrc$1@speranza.aioe.org>
In reply to#23671
Em 25/03/2014 15:24, jonas.thornvall@gmail.com escreveu:
> In python i beleive mathematical expression can be directly
> evaluated, if in correct form.

In modern browsers, we may use the JavaScript console, the Developer 
Tools (press F12), or even the browser's address bar (e.g. javascript: 2 
+ 3)

In Node.js, we can use the command line.

>
> Javascript do not have eval?

There is a built-in eval function in JavaScript (JS), but it may be 
evil, or not. See 
<http://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/>

The resemblances between Python and JS are misleading. For instance, in 
Python we have different types of Numbers, and Python Lists look like 
Arrays in JS, but indexing in Python is more flexible than in JS. E.g. 
arr[-1] in Python returns the last item in the list, but in JS it would 
return undefined, unless there was a property named "-1" in the Array 
Object (arr).

>
> If not, how do one go ahead evaluate something with two constant
> operands but where the operator is a variable?
>
> z=*;   x=3z5
>
> z=/    x=3z3
>
> z=+    x=3z3
>
> z=-    x=3z3
>

These are invalid assignments in JS.

In the other hand, there are Punctuators in JS +=, -=, *=, =/, etc.

>
> Will there be a eval function in future javascript?
>

See above.

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#23679

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-03-25 23:49 +0100
Message-ID<533207f1$0$4217$9b4e6d93@newsspool2.arcor-online.net>
In reply to#23671
jonas.thornvall@gmail.com wrote:

> If not, how do one go ahead evaluate something with two constant
> operands but where the operator is a variable?
> 
> z=*;   x=3z5
> 
> z=/    x=3z3
> 
> z=+    x=3z3
> 
> z=-    x=3z3

You may consider using functions instead of operators.  Then you can do
something like the following:

  function add(a, b) {return a + b;}
  function mul(a, b) {return a * b;}

  var op = add;
  alert(op(3, 5)); // 8
  op = mul;
  alert(op(3, 5)); // 15

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#23680

Fromjonas.thornvall@gmail.com
Date2014-03-25 22:43 -0700
Message-ID<18bbc7cb-380c-438b-a5b6-285bd94fafd9@googlegroups.com>
In reply to#23679
Den tisdagen den 25:e mars 2014 kl. 23:49:23 UTC+1 skrev Christoph Michael Becker:
> jonas.thornvall@gmail.com wrote:
> 
> 
> 
> > If not, how do one go ahead evaluate something with two constant
> 
> > operands but where the operator is a variable?
> 
> > 
> 
> > z=*;   x=3z5
> 
> > 
> 
> > z=/    x=3z3
> 
> > 
> 
> > z=+    x=3z3
> 
> > 
> 
> > z=-    x=3z3
> 
> 
> 
> You may consider using functions instead of operators.  Then you can do
> 
> something like the following:
> 
> 
> 
>   function add(a, b) {return a + b;}
> 
>   function mul(a, b) {return a * b;}
> 
> 
> 
>   var op = add;
> 
>   alert(op(3, 5)); // 8
> 
>   op = mul;
> 
>   alert(op(3, 5)); // 15
> 
> 
> 
> -- 
> 
> Christoph M. Becker

I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 

strMath="x^y+uv-s"
evaluated=strMath.calc();

Surely someone must have implemented such a function already?

[toc] | [prev] | [next] | [standalone]


#23681

Fromjonas.thornvall@gmail.com
Date2014-03-25 23:29 -0700
Message-ID<f63e0028-4084-43f5-9b49-43c1df692351@googlegroups.com>
In reply to#23680
Den onsdagen den 26:e mars 2014 kl. 06:43:05 UTC+1 skrev jonas.t...@gmail.com:
> Den tisdagen den 25:e mars 2014 kl. 23:49:23 UTC+1 skrev Christoph Michael Becker:
> 
> > jonas.thornvall@gmail.com wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > > If not, how do one go ahead evaluate something with two constant
> 
> > 
> 
> > > operands but where the operator is a variable?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > z=*;   x=3z5
> 
> > 
> 
> > > 
> 
> > 
> 
> > > z=/    x=3z3
> 
> > 
> 
> > > 
> 
> > 
> 
> > > z=+    x=3z3
> 
> > 
> 
> > > 
> 
> > 
> 
> > > z=-    x=3z3
> 
> > 
> 
> > 
> 
> > 
> 
> > You may consider using functions instead of operators.  Then you can do
> 
> > 
> 
> > something like the following:
> 
> > 
> 
> > 
> 
> > 
> 
> >   function add(a, b) {return a + b;}
> 
> > 
> 
> >   function mul(a, b) {return a * b;}
> 
> > 
> 
> > 
> 
> > 
> 
> >   var op = add;
> 
> > 
> 
> >   alert(op(3, 5)); // 8
> 
> > 
> 
> >   op = mul;
> 
> > 
> 
> >   alert(op(3, 5)); // 15
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Christoph M. Becker
> 
> 
> 
> I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 
> 
> 
> 
> strMath="x^y+uv-s"
> 
> evaluated=strMath.calc();
> 
> 
> 
> Surely someone must have implemented such a function already?

I really do not need a full library, just the standard four operators.

If one use a library like math.js, does it load with the main script and stay in memory?
And most the expressson string be global, or is it passed like a parameter and returned?

Maybe i try to implement something.

[toc] | [prev] | [next] | [standalone]


#23721

FromJim T. <x@y.z>
Date2014-03-28 11:48 -0500
Message-ID<3t9bj99pnkfho00kc5oraphmajc8m9em8o@4ax.com>
In reply to#23681
On Tue, 25 Mar 2014 23:29:34 -0700 (PDT), jonas.thornvall@gmail.com
wrote:

>Den onsdagen den 26:e mars 2014 kl. 06:43:05 UTC+1 skrev jonas.t...@gmail.com:
>> Den tisdagen den 25:e mars 2014 kl. 23:49:23 UTC+1 skrev Christoph Michael Becker:
>> 
>> > jonas.thornvall@gmail.com wrote:
>> 
>> > 
>> 
>> > 
>> 
>> > 
>> 
>> > > If not, how do one go ahead evaluate something with two constant
>> 
>> > 
>> 
>> > > operands but where the operator is a variable?
>> 
>> > 
>> 
>> > > 
>> 
>> > 
>> 
>> > > z=*;   x=3z5
>> 
>> > 
>> 
>> > > 
>> 
>> > 
>> 
>> > > z=/    x=3z3
>> 
>> > 
>> 
>> > > 
>> 
>> > 
>> 
>> > > z=+    x=3z3
>> 
>> > 
>> 
>> > > 
>> 
>> > 
>> 
>> > > z=-    x=3z3
>> 
>> > 
>> 
>> > 
>> 
>> > 
>> 
>> > You may consider using functions instead of operators.  Then you can do
>> 
>> > 
>> 
>> > something like the following:
>> 
>> > 
>> 
>> > 
>> 
>> > 
>> 
>> >   function add(a, b) {return a + b;}
>> 
>> > 
>> 
>> >   function mul(a, b) {return a * b;}
>> 
>> > 
>> 
>> > 
>> 
>> > 
>> 
>> >   var op = add;
>> 
>> > 
>> 
>> >   alert(op(3, 5)); // 8
>> 
>> > 
>> 
>> >   op = mul;
>> 
>> > 
>> 
>> >   alert(op(3, 5)); // 15
>> 
>> > 
>> 
>> > 
>> 
>> > 
>> 
>> > -- 
>> 
>> > 
>> 
>> > Christoph M. Becker
>> 
>> 
>> 
>> I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 
>> 
>> 
>> 
>> strMath="x^y+uv-s"
>> 
>> evaluated=strMath.calc();
>> 
>> 
>> 
>> Surely someone must have implemented such a function already?
>
>I really do not need a full library, just the standard four operators.
>
>If one use a library like math.js, does it load with the main script and stay in memory?
>And most the expressson string be global, or is it passed like a parameter and returned?
>
>Maybe i try to implement something.

Am I missing something? Won't the plain old eval() function do what
you want?

[toc] | [prev] | [next] | [standalone]


#23725

Fromjonas.thornvall@gmail.com
Date2014-03-28 10:03 -0700
Message-ID<a4cd2804-db95-4cab-9f2b-c9b56206d4ca@googlegroups.com>
In reply to#23721
Den fredagen den 28:e mars 2014 kl. 16:47:56 UTC+1 skrev Jim T.:
> On Tue, 25 Mar 2014 23:29:34 -0700 (PDT), jonas.thornvall@gmail.com
> 
> wrote:
> 
> 
> 
> >Den onsdagen den 26:e mars 2014 kl. 06:43:05 UTC+1 skrev jonas.t...@gmail.com:
> 
> >> Den tisdagen den 25:e mars 2014 kl. 23:49:23 UTC+1 skrev Christoph Michael Becker:
> 
> >> 
> 
> >> > jonas.thornvall@gmail.com wrote:
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > If not, how do one go ahead evaluate something with two constant
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > operands but where the operator is a variable?
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > z=*;   x=3z5
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > z=/    x=3z3
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > z=+    x=3z3
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > > z=-    x=3z3
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > You may consider using functions instead of operators.  Then you can do
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > something like the following:
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> >   function add(a, b) {return a + b;}
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> >   function mul(a, b) {return a * b;}
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> >   var op = add;
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> >   alert(op(3, 5)); // 8
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> >   op = mul;
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> >   alert(op(3, 5)); // 15
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > -- 
> 
> >> 
> 
> >> > 
> 
> >> 
> 
> >> > Christoph M. Becker
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> strMath="x^y+uv-s"
> 
> >> 
> 
> >> evaluated=strMath.calc();
> 
> >> 
> 
> >> 
> 
> >> 
> 
> >> Surely someone must have implemented such a function already?
> 
> >
> 
> >I really do not need a full library, just the standard four operators.
> 
> >
> 
> >If one use a library like math.js, does it load with the main script and stay in memory?
> 
> >And most the expressson string be global, or is it passed like a parameter and returned?
> 
> >
> 
> >Maybe i try to implement something.
> 
> 
> 
> Am I missing something? Won't the plain old eval() function do what
> 
> you want?

It seem it actually does some part +-*/ in correct order at least with the few operands i've tried, but i do not think it do powers..
At least i can not get it to evaluate 2^2+1

[toc] | [prev] | [next] | [standalone]


#23726

FromJim T. <x@y.z>
Date2014-03-28 13:12 -0500
Message-ID<aoebj9llfs0o012iuk8a5rsp2n79mcnk3e@4ax.com>
In reply to#23725
On Fri, 28 Mar 2014 10:03:32 -0700 (PDT), jonas.thornvall@gmail.com
wrote:

>Den fredagen den 28:e mars 2014 kl. 16:47:56 UTC+1 skrev Jim T.:
>> On Tue, 25 Mar 2014 23:29:34 -0700 (PDT), jonas.thornvall@gmail.com
>> >Maybe i try to implement something.
>> 
>> 
>> 
>> Am I missing something? Won't the plain old eval() function do what
>> 
>> you want?
>
>It seem it actually does some part +-*/ in correct order at least with the few operands i've tried, but i do not think it do powers..
>At least i can not get it to evaluate 2^2+1

JavaScript doesn't have a power operator, but you can use Math.pow()
instead.

[toc] | [prev] | [next] | [standalone]


#23728

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-03-28 18:33 +0100
Message-ID<XnsA2FEBCBAAD36Beejj99@194.109.133.133>
In reply to#23725
jonas.thornvall@gmail.com wrote on 28 mrt 2014 in comp.lang.javascript:

> It seem it actually does some part +-*/ in correct order at least with
> the few operands i've tried, but i do not think it do powers.. At least
> i can not get it to evaluate 2^2+1 

^ is the bitwise xor operator, 
works fine here:

eval('2^2+1') gives: 1

You might think the power operator being more important,
methinks not.

===============================

This has noting to do with eval() as such, as:

alert( 2^2+1 ) gives: 1

===============================

try:

var result = Math.pow(base, exponent);

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23730

Fromjonas.thornvall@gmail.com
Date2014-03-28 10:50 -0700
Message-ID<86447148-62cb-46d4-8409-672376aaeeb6@googlegroups.com>
In reply to#23728
Den fredagen den 28:e mars 2014 kl. 18:33:10 UTC+1 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 28 mrt 2014 in comp.lang.javascript:
> 
> 
> 
> > It seem it actually does some part +-*/ in correct order at least with
> 
> > the few operands i've tried, but i do not think it do powers.. At least
> 
> > i can not get it to evaluate 2^2+1 
> 
> 
> 
> ^ is the bitwise xor operator, 
> 
> works fine here:
> 
> 
> 
> eval('2^2+1') gives: 1
> 
> 
> 
> You might think the power operator being more important,
> 
> methinks not.
> 
> 
> 
> ===============================
> 
> 
> 
> This has noting to do with eval() as such, as:
> 
> 
> 
> alert( 2^2+1 ) gives: 1
> 
> 
> 
> ===============================
> 
> 
> 
> try:
> 
> 
> 
> var result = Math.pow(base, exponent);
> 
> 
> 
> -- 
> 
> Evertjan.
> 
> The Netherlands.
> 
> (Please change the x'es to dots in my emailaddress)

Really i had no idea that 3^3 = 0; both surprising and new to me.

[toc] | [prev] | [next] | [standalone]


#23731

Fromjonas.thornvall@gmail.com
Date2014-03-28 10:59 -0700
Message-ID<797714b6-c037-4ec1-aa83-ef179e13ac05@googlegroups.com>
In reply to#23730
Den fredagen den 28:e mars 2014 kl. 18:50:29 UTC+1 skrev jonas.t...@gmail.com:
> Den fredagen den 28:e mars 2014 kl. 18:33:10 UTC+1 skrev Evertjan.:
> 
> > jonas.thornvall@gmail.com wrote on 28 mrt 2014 in comp.lang.javascript:
> 
> > 
> 
> > 
> 
> > 
> 
> > > It seem it actually does some part +-*/ in correct order at least with
> 
> > 
> 
> > > the few operands i've tried, but i do not think it do powers.. At least
> 
> > 
> 
> > > i can not get it to evaluate 2^2+1 
> 
> > 
> 
> > 
> 
> > 
> 
> > ^ is the bitwise xor operator, 
> 
> > 
> 
> > works fine here:
> 
> > 
> 
> > 
> 
> > 
> 
> > eval('2^2+1') gives: 1
> 
> > 
> 
> > 
> 
> > 
> 
> > You might think the power operator being more important,
> 
> > 
> 
> > methinks not.
> 
> > 
> 
> > 
> 
> > 
> 
> > ===============================
> 
> > 
> 
> > 
> 
> > 
> 
> > This has noting to do with eval() as such, as:
> 
> > 
> 
> > 
> 
> > 
> 
> > alert( 2^2+1 ) gives: 1
> 
> > 
> 
> > 
> 
> > 
> 
> > ===============================
> 
> > 
> 
> > 
> 
> > 
> 
> > try:
> 
> > 
> 
> > 
> 
> > 
> 
> > var result = Math.pow(base, exponent);
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Evertjan.
> 
> > 
> 
> > The Netherlands.
> 
> > 
> 
> > (Please change the x'es to dots in my emailaddress)
> 
> 
> 
> Really i had no idea that 3^3 = 0; both surprising and new to me.

Joke aside there is alot of things i am fond of in ECMAscript the way it is implemented towards functonality in browser and how it can interact with HTML pages in a very simple way, but i honestly can not see why it can not evaluate simple arithmetic expressions without building a parser. A bit silly don't you think. I do know it is not a calculator but one would think it would be able to perform on level with a simple hand calculator.

[toc] | [prev] | [next] | [standalone]


#23736

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2014-03-28 20:38 +0100
Message-ID<XnsA2FED1E97C50Aeejj99@194.109.133.133>
In reply to#23731
jonas.thornvall@gmail.com wrote on 28 mrt 2014 in comp.lang.javascript:
> I wrote:
>> Really i had no idea that 3^3 = 0; both surprising and new to me.
> 
> Joke aside there is alot of things i am fond of in ECMAscript the way it
> is implemented towards functonality in browser 

This is no joke. It seems you are very young, 
and still think that the world should be shaped for your benefit.

Javascript is not ment to parse user strings, but to be a nice general 
language for diverse engines.

> and how it can interact with HTML pages in a very simple way,

No, native Javascript does not. You are discussing DOM functions, that are 
not part of Javascript, just extentions. They could be extentions into other 
languages too. VBScript used to be one of them in IE, till IE11. 

Cscript/Jscript, for instance, has no DOM-functions at all.

> but i honestly can not see why it
> can not evaluate simple arithmetic expressions without building a
> parser. 

I fail to see what this has to do with your honesty.

A computer language is made to execute programme scripts,
not to execute user strings.

> A bit silly don't you think.

No. [Well yes, methinks the way you act is].
You don't seem to understand what a scripting language is for.

There is a perfectly good function to execute power, as you were shown here.

> I do know it is not a calculator
> but one would think it would be able to perform on level with a simple
> hand calculator. 

Javascript, while having a rather good garbage collector, 
cannot natively clean your room by vacuuming.

It could clean your desktop using the cscript/wscript engine, btw. 

A hand calculator is build for user interaction, Javascript is not.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#23744

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2014-03-29 19:49 -0700
Message-ID<7d60fce5-e6ba-4573-bd5c-ed7b9fdd5bd5@googlegroups.com>
In reply to#23731
On Friday, March 28, 2014 12:59:09 PM UTC-5, jonas.t...@gmail.com wrote:
> Joke aside there is alot of things i am fond of in ECMAscript the way it is implemented towards functonality in browser and how it can interact with HTML pages in a very simple way, but i honestly can not see why it can not evaluate simple arithmetic expressions without building a parser. A bit silly don't you think. I do know it is not a calculator but one would think it would be able to perform on level with a simple hand calculator.

It _does_ evaluate simple arithmetic expressions without building a parser (assuming it is legal JavaScript in the first place). If you want to execute an expression which is not legal JavaScript, you will indeed need a parser for this.

I don't see why the claim silly is made. Silly that JavaScript doesn't have feature X?

[toc] | [prev] | [next] | [standalone]


#23740

FromEd Jay <edMbj@aes-intl.com>
Date2014-03-29 10:53 -0700
Message-ID<h71ej9lo792i6nf7vi0lrdiml9of00gp7n@4ax.com>
In reply to#23730
jonas.thornvall@gmail.com wrote:

> Evertjan wrote:
>> try:
>> var result = Math.pow(base, exponent);
>> 
>Really i had no idea that 3^3 = 0; both surprising and new to me.

What does 3^3 mean in javascript? 

Math.pow(3,3) = 9 != 3^3.

-- 
Ed Jay (remove 'M' to reply by email)

Win the War Against Breast Cancer.
Knowing the facts could save your life. 
http://www.breastthermography.info

[toc] | [prev] | [next] | [standalone]


#23743

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2014-03-29 19:45 -0700
Message-ID<afc859d9-8de9-4639-94ba-8eafd468af38@googlegroups.com>
In reply to#23680
On Wednesday, March 26, 2014 12:43:05 AM UTC-5, jonas.t...@gmail.com wrote:

> I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 
> 
> strMath="x^y+uv-s"
> 
> evaluated=strMath.calc();
> 
> Surely someone must have implemented such a function already?

Implementing this for yourself should be relatively straightforward. One example of a parsing function as a starting point:
<http://rosettacode.org/wiki/Arithmetic_evaluation#JavaScript>

Here is a varied example of the given above:

function calc(str){
    return eval(str);
}

var x = 5, y = 2, u = 9, v = 4, s = 7;

var strMath="x*y+u/v-s";

var evaluated = calc(strMath);

[toc] | [prev] | [next] | [standalone]


#23746

Fromjonas.thornvall@gmail.com
Date2014-03-30 09:21 -0700
Message-ID<7fbef56a-e8c7-40a4-9ec6-9f22bc4b306a@googlegroups.com>
In reply to#23743
Den söndagen den 30:e mars 2014 kl. 04:45:33 UTC+2 skrev Michael Haufe (TNO):
> On Wednesday, March 26, 2014 12:43:05 AM UTC-5, jonas.t...@gmail.com wrote:
> 
> 
> 
> > I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 
> 
> > 
> 
> > strMath="x^y+uv-s"
> 
> > 
> 
> > evaluated=strMath.calc();
> 
> > 
> 
> > Surely someone must have implemented such a function already?
> 
> 
> 
> Implementing this for yourself should be relatively straightforward. One example of a parsing function as a starting point:
> 
> <http://rosettacode.org/wiki/Arithmetic_evaluation#JavaScript>
> 
> 
> 
> Here is a varied example of the given above:
> 
> 
> 
> function calc(str){
> 
>     return eval(str);
> 
> }
> 
> 
> 
> var x = 5, y = 2, u = 9, v = 4, s = 7;
> 
> 
> 
> var strMath="x*y+u/v-s";
> 
> 
> 
> var evaluated = calc(strMath);

Best tip given so far. I think i use this and maybe later implement my own when i need doing arithmetic in other bases.

[toc] | [prev] | [next] | [standalone]


#23747

Fromjonas.thornvall@gmail.com
Date2014-03-30 09:24 -0700
Message-ID<db9229be-a7c8-411b-9549-1077d0e7043a@googlegroups.com>
In reply to#23743
Den söndagen den 30:e mars 2014 kl. 04:45:33 UTC+2 skrev Michael Haufe (TNO):
> On Wednesday, March 26, 2014 12:43:05 AM UTC-5, jonas.t...@gmail.com wrote:
> 
> 
> 
> > I was hoping for a math parser that could evaluate a mathematical expression in correct order, where you simply send the variable operands as a string broken down and calculated like 
> 
> > 
> 
> > strMath="x^y+uv-s"
> 
> > 
> 
> > evaluated=strMath.calc();
> 
> > 
> 
> > Surely someone must have implemented such a function already?
> 
> 
> 
> Implementing this for yourself should be relatively straightforward. One example of a parsing function as a starting point:
> 
> <http://rosettacode.org/wiki/Arithmetic_evaluation#JavaScript>
> 
> 
> 
> Here is a varied example of the given above:
> 
> 
> 
> function calc(str){
> 
>     return eval(str);
> 
> }
> 
> 
> 
> var x = 5, y = 2, u = 9, v = 4, s = 7;
> 
> 
> 
> var strMath="x*y+u/v-s";
> 
> 
> 
> var evaluated = calc(strMath);

Is there any convention how one should acknowledge that one copied the code from the Rosetta site?

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web