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


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

My code find negative squares buggy?

Started byjonas.thornvall@gmail.com
First post2014-01-10 07:40 -0800
Last post2014-01-10 17:58 +0100
Articles 7 — 3 participants

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


Contents

  My code find negative squares buggy? jonas.thornvall@gmail.com - 2014-01-10 07:40 -0800
    Re: My code find negative squares buggy? jonas.thornvall@gmail.com - 2014-01-10 07:44 -0800
    Re: My code find negative squares buggy? Tim Streater <timstreater@greenbee.net> - 2014-01-10 15:52 +0000
      Re: My code find negative squares buggy? jonas.thornvall@gmail.com - 2014-01-10 08:09 -0800
        Re: My code find negative squares buggy? Tim Streater <timstreater@greenbee.net> - 2014-01-10 16:19 +0000
          Re: My code find negative squares buggy? jonas.thornvall@gmail.com - 2014-01-10 08:32 -0800
        Re: My code find negative squares buggy? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-01-10 17:58 +0100

#22549 — My code find negative squares buggy?

Fromjonas.thornvall@gmail.com
Date2014-01-10 07:40 -0800
SubjectMy code find negative squares buggy?
Message-ID<ed1316d2-3b87-4a4a-901a-05f6131e3e4f@googlegroups.com>
This works fine
square=Math.sqrt(number,2);
  if(square-Math.floor(square)== 0){ctx.fillStyle = 'red';}

This does not?

negsquare=Math.sqrt(-number,2);
if(number<0 && negsquare-Math.floor(negsquare)== 0) {ctx.fillStyle = 'orange';}

For complete example see
file:///C:/Users/jonas/Desktop/prime0.4/main.html

[toc] | [next] | [standalone]


#22550

Fromjonas.thornvall@gmail.com
Date2014-01-10 07:44 -0800
Message-ID<03142f1f-7c64-4fde-a992-e56ccd0e2d69@googlegroups.com>
In reply to#22549
Den fredagen den 10:e januari 2014 kl. 16:40:49 UTC+1 skrev jonas.t...@gmail.com:
> This works fine
> 
> square=Math.sqrt(number,2);
> 
>   if(square-Math.floor(square)== 0){ctx.fillStyle = 'red';}
> 
> 
> 
> This does not?
> 
> 
> 
> negsquare=Math.sqrt(-number,2);
> 
> if(number<0 && negsquare-Math.floor(negsquare)== 0) {ctx.fillStyle = 'orange';}
> 
> 
> 
> For complete example see
> 
> file:///C:/Users/jonas/Desktop/prime0.4/main.html

Sorry
http://web.comhem.se/jonasth/prime0.4/main.html

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


#22551

FromTim Streater <timstreater@greenbee.net>
Date2014-01-10 15:52 +0000
Message-ID<100120141552439734%timstreater@greenbee.net>
In reply to#22549
In article <ed1316d2-3b87-4a4a-901a-05f6131e3e4f@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:

> This works fine
> square=Math.sqrt(number,2);
>   if(square-Math.floor(square)== 0){ctx.fillStyle = 'red';}
> 
> This does not?
> 
> negsquare=Math.sqrt(-number,2);

What does negsquare get set to?

> if(number<0 && negsquare-Math.floor(negsquare)== 0) {ctx.fillStyle =
> 'orange';}

And what does Math.floor do with whatever negsquare got set to above?

-- 
"The idea that Bill Gates has appeared like a knight in shining armour to
 lead all customers out of a mire of technological chaos neatly ignores 
 the fact that it was he who, by peddling second-rate technology, led them
 into it in the first place."                             - Douglas Adams

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


#22552

Fromjonas.thornvall@gmail.com
Date2014-01-10 08:09 -0800
Message-ID<d1e23965-ac9e-4bd7-9d3d-3af30643f383@googlegroups.com>
In reply to#22551
Den fredagen den 10:e januari 2014 kl. 16:52:43 UTC+1 skrev Tim Streater:
> In article <ed1316d2-3b87-4a4a-901a-05f6131e3e4f@googlegroups.com>,
> 
> <jonas.thornvall@gmail.com> wrote:
> 
> 
> 
> > This works fine
> 
> > square=Math.sqrt(number,2);
> 
> >   if(square-Math.floor(square)== 0){ctx.fillStyle = 'red';}
> 
> > 
> 
> > This does not?
> 
> > 
> 
> > negsquare=Math.sqrt(-number,2);
> 

> 
> What does negsquare get set to?
> 
> 
> 
> > if(number<0 && negsquare-Math.floor(negsquare)== 0) {ctx.fillStyle =
> 
> > 'orange';}
> 
> 
> 
> And what does Math.floor do with whatever negsquare got set to above?
> 
> 
> 
> -- 
> 
> "The idea that Bill Gates has appeared like a knight in shining armour to
> 
>  lead all customers out of a mire of technological chaos neatly ignores 
> 
>  the fact that it was he who, by peddling second-rate technology, led them
> 
>  into it in the first place."                             - Douglas Adams

If number is -50
negsquare=Math.sqrt(-50,2);

negsquare=7,0710678118654752440084436210485 

Or? Isnt't sqrt=Math.sqrt(number,2); equivalent with negsqrt=Math.sqrt(-number,2); given that number is negative?

Which gives

if(-50<0 && 7,0710678118654752440084436210485-Math.floor(7,0710678118654752440084436210485 
)== 0) {ctx.fillStyle ='orange';}

At least that is what i though it would do?


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


#22553

FromTim Streater <timstreater@greenbee.net>
Date2014-01-10 16:19 +0000
Message-ID<100120141619527449%timstreater@greenbee.net>
In reply to#22552
In article <d1e23965-ac9e-4bd7-9d3d-3af30643f383@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:

> Den fredagen den 10:e januari 2014 kl. 16:52:43 UTC+1 skrev Tim Streater:
> > In article <ed1316d2-3b87-4a4a-901a-05f6131e3e4f@googlegroups.com>,
> > 
> > <jonas.thornvall@gmail.com> wrote:

> > > This works fine
> > > square=Math.sqrt(number,2);
> > >   if(square-Math.floor(square)== 0){ctx.fillStyle = 'red';}

> > > This does not?
> > > negsquare=Math.sqrt(-number,2);

> > What does negsquare get set to?

> > > if(number<0 && negsquare-Math.floor(negsquare)== 0) {ctx.fillStyle =
> > > 'orange';}

> > And what does Math.floor do with whatever negsquare got set to above?

> If number is -50
> negsquare=Math.sqrt(-50,2);

But suppose number is +50. What does your code do then?

> negsquare=7,0710678118654752440084436210485 
> 
> Or? Isnt't sqrt=Math.sqrt(number,2); equivalent with
> negsqrt=Math.sqrt(-number,2); given that number is negative?
> 
> Which gives
> 
> if(-50<0 && 7,0710678118654752440084436210485-Math.floor(7,0710678118654752440084436210485 
> )== 0) {ctx.fillStyle ='orange';}
> 
> At least that is what i though it would do?

I expect you need to check operator precedence in your "if".

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

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


#22554

Fromjonas.thornvall@gmail.com
Date2014-01-10 08:32 -0800
Message-ID<f4eca250-19ec-4e10-be90-58e7d716a70c@googlegroups.com>
In reply to#22553
Den fredagen den 10:e januari 2014 kl. 17:19:52 UTC+1 skrev Tim Streater:
> In article <d1e23965-ac9e-4bd7-9d3d-3af30643f383@googlegroups.com>,
> 
> <jonas.thornvall@gmail.com> wrote:
> 
> 
> 
> > Den fredagen den 10:e januari 2014 kl. 16:52:43 UTC+1 skrev Tim Streater:
> 
> > > In article <ed1316d2-3b87-4a4a-901a-05f6131e3e4f@googlegroups.com>,
> 
> > > 
> 
> > > <jonas.thornvall@gmail.com> wrote:
> 
> 
> 
> > > > This works fine
> 
> > > > square=Math.sqrt(number,2);
> 
> > > >   if(square-Math.floor(square)== 0){ctx.fillStyle = 'red';}
> 
> 
> 
> > > > This does not?
> 
> > > > negsquare=Math.sqrt(-number,2);
> 
> 
> 
> > > What does negsquare get set to?
> 
> 
> 
> > > > if(number<0 && negsquare-Math.floor(negsquare)== 0) {ctx.fillStyle =
> 
> > > > 'orange';}
> 
> 
> 
> > > And what does Math.floor do with whatever negsquare got set to above?
> 
> 
> 
> > If number is -50
> 
> > negsquare=Math.sqrt(-50,2);
> 
> 
> 
> But suppose number is +50. What does your code do then?
> 
> 
> 
> > negsquare=7,0710678118654752440084436210485 
> 
> > 
> 
> > Or? Isnt't sqrt=Math.sqrt(number,2); equivalent with
> 
> > negsqrt=Math.sqrt(-number,2); given that number is negative?
> 
> > 
> 
> > Which gives
> 
> > 
> 
> > if(-50<0 && 7,0710678118654752440084436210485-Math.floor(7,0710678118654752440084436210485 
> 
> > )== 0) {ctx.fillStyle ='orange';}
> 
> > 
> 
> > At least that is what i though it would do?
> 
> 
> 
> I expect you need to check operator precedence in your "if".
> 
> 
> 
> -- 
> 
> Tim
> 
> 
> 
> "That excessive bail ought not to be required, nor excessive fines imposed,
> 
> nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

Maybe i change the value in the main loop, i think that is it thanks.

I found it i add upp number inside main loop twice.

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


#22555

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-01-10 17:58 +0100
Message-ID<52d026be$0$9504$9b4e6d93@newsspool1.arcor-online.net>
In reply to#22552
jonas.thornvall@gmail.com wrote:

> Or? Isnt't sqrt=Math.sqrt(number,2); equivalent with
> negsqrt=Math.sqrt(-number,2); given that number is negative?

The square root of negative numbers is not defined for reals (the result
would be a complex number).[1]  In conforming ECMAScript implementations
Math.sqrt() of a negative number returns NaN.[2]

> Which gives
> 
> if(-50<0 &&
> 7,0710678118654752440084436210485-Math.floor(7,0710678118654752440084436210485
>  )== 0) {ctx.fillStyle ='orange';}

Testing floating point numbers for equality is unreliable.[3]

> At least that is what i though it would do?

Please read and heed the newsgroup's rules on posting and replying[4].

[1] <http://en.wikipedia.org/wiki/Square_root>
[2] <http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.17>
[3] <http://pointedears.de/scripts/faq/cljs/#binaryNumbers>
[4] <http://pointedears.de/scripts/faq/cljs/notes/posting/>

-- 
Christoph M. Becker

[toc] | [prev] | [standalone]


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


csiph-web