RE: [Flashcoders] Strange behaviour from modulus operator
Try using Math.ceil or Math.floor just in case. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Steven Sent: 06 March 2006 14:03 To: Flashcoders mailing list Subject: [Flashcoders] Strange behaviour from modulus operator Experiencing a strange problem using the modulus operator. Inside a function I have the following code: var vValue1 = passed_MC._x - this.Maze_Horizontal_offset; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vValue1 = " + vValue1); trace ("vTheResult = " + Number(vTheResult)); And here are the results I am getting from the trace statements: vValue1 = 324 vTheResult = 5.6843418860808e-14 Surely it should give me a result of zero as 36 goes into 324 exactly 9 times. The weird thing is if I just hard code the values as follows, it works var vValue1 = 324; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vTheResult = " + Number(vTheResult)); - And here is the result of the trace statement: vTheResult = 0 I am therefore totally confused!!! Any help much appreciated. I am publishing for Flash 7 with Actionscript 1.0 Thanks Paul ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Strange behaviour from modulus operator
How much closer to zero where you expecting? 5 to the power of 10 to the -14 is zero to the limits of modern PCs. If you want to deal with integers make the numbers integers. If you do your math in floating point with a mantissa with lots of bits, you are going to get some bits left over at the end. The machine is doing all of its math in binary and some/many decimal numbers can not be represented exactly in binary. Think of 1/3+1/3+1/3 != .33+.33+.33 It is not just machines that have the problem. You can never compare a floating point number to zero and expect your code to work reliably. Convert the result of modulus to an integer and compare that to 0. Ron Paul Steven wrote: Experiencing a strange problem using the modulus operator. Inside a function I have the following code: var vValue1 = passed_MC._x - this.Maze_Horizontal_offset; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vValue1 = " + vValue1); trace ("vTheResult = " + Number(vTheResult)); And here are the results I am getting from the trace statements: vValue1 = 324 vTheResult = 5.6843418860808e-14 Surely it should give me a result of zero as 36 goes into 324 exactly 9 times. The weird thing is if I just hard code the values as follows, it works var vValue1 = 324; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vTheResult = " + Number(vTheResult)); - And here is the result of the trace statement: vTheResult = 0 I am therefore totally confused!!! Any help much appreciated. I am publishing for Flash 7 with Actionscript 1.0 Thanks Paul ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
RE: [Flashcoders] Strange behaviour from modulus operator
Thanks a million Helen - I have been sitting for hours trying to figure this one out - doesnt help being bunged up with the cold so few brain cells actually functioning!! -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Helen Triolo Sent: 06 March 2006 14:13 To: Flashcoders mailing list Subject: Re: [Flashcoders] Strange behaviour from modulus operator Because _x is not necessarily an integer, vValue1 is not guaranteed to be an integer either, which it must be for modulo to return the result you want. Force it to be with var vValue1 = Math.round(passed_MC._x - this.Maze_Horizontal_offset); and it should work as you expect. Helen -- http://flash-creations.com http://i-technica.com Paul Steven wrote: >Experiencing a strange problem using the modulus operator. > >Inside a function I have the following code: > >var vValue1 = passed_MC._x - this.Maze_Horizontal_offset; >var vValue2 = 36; > >var vTheResult = vValue1 % vValue2; > >trace ("vValue1 = " + vValue1); >trace ("vTheResult = " + Number(vTheResult)); > >And here are the results I am getting from the trace statements: > >vValue1 = 324 >vTheResult = 5.6843418860808e-14 > > > >Surely it should give me a result of zero as 36 goes into 324 exactly 9 >times. > >The weird thing is if I just hard code the values as follows, it works > >var vValue1 = 324; >var vValue2 = 36; > >var vTheResult = vValue1 % vValue2; > >trace ("vTheResult = " + Number(vTheResult)); > >- > >And here is the result of the trace statement: > >vTheResult = 0 > >I am therefore totally confused!!! > >Any help much appreciated. I am publishing for Flash 7 with Actionscript 1.0 > >Thanks > >Paul > > ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Strange behaviour from modulus operator
Experiencing a strange problem using the modulus operator. Inside a function I have the following code: var vValue1 = passed_MC._x - this.Maze_Horizontal_offset; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vValue1 = " + vValue1); trace ("vTheResult = " + Number(vTheResult)); And here are the results I am getting from the trace statements: vValue1 = 324 vTheResult = 5.6843418860808e-14 It's a result of rounding errors. The _x value is a float, and so there's a very small leftover value at the bottom end of the decimal places. Notice that your vTheResult is very, very small. You can avoid the problem by taking Math.round(vValue1) % vValue2. Danny ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Strange behaviour from modulus operator
Because _x is not necessarily an integer, vValue1 is not guaranteed to be an integer either, which it must be for modulo to return the result you want. Force it to be with var vValue1 = Math.round(passed_MC._x - this.Maze_Horizontal_offset); and it should work as you expect. Helen -- http://flash-creations.com http://i-technica.com Paul Steven wrote: Experiencing a strange problem using the modulus operator. Inside a function I have the following code: var vValue1 = passed_MC._x - this.Maze_Horizontal_offset; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vValue1 = " + vValue1); trace ("vTheResult = " + Number(vTheResult)); And here are the results I am getting from the trace statements: vValue1 = 324 vTheResult = 5.6843418860808e-14 Surely it should give me a result of zero as 36 goes into 324 exactly 9 times. The weird thing is if I just hard code the values as follows, it works var vValue1 = 324; var vValue2 = 36; var vTheResult = vValue1 % vValue2; trace ("vTheResult = " + Number(vTheResult)); - And here is the result of the trace statement: vTheResult = 0 I am therefore totally confused!!! Any help much appreciated. I am publishing for Flash 7 with Actionscript 1.0 Thanks Paul ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com