It is currently April 28th, 2024, 11:13 am

Formula not returning expected result.

Get help with creating, editing & fixing problems with skins
MrJackFrost
Posts: 18
Joined: May 25th, 2012, 8:41 pm

Formula not returning expected result.

Post by MrJackFrost »

[Measure-FathersDay]
Measure=Calc
Formula=15+(-1*(((2500)+11)%7))%7

[Meter-FathersDay]
Meter=String
MeasureName=Measure-FathersDay
FontColor=255,255,255,255
Text=%1

Displays 10 but I expected it to display 17?

The link below shows Google's calculator also expecting 17 to be the answer.
Where am I going wrong?

http://www.google.com/search?q=15%2B%28-1*%28%282500%2B11%29%257%29%29%257&btnG=Search
User avatar
Brian
Developer
Posts: 2688
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Formula not returning expected result.

Post by Brian »

Seems like google is wrong.

15+(-1*(((2500)+11)%7))%7

2500+11 = 2511
2511%7 = 5
-1 * 5 = -5
-5%7 = -5
15+(-5) = 10

Damn google.

-Brian
MrJackFrost
Posts: 18
Joined: May 25th, 2012, 8:41 pm

Re: Formula not returning expected result.

Post by MrJackFrost »

Hmmm,

Running the same thing in Python is returning 17

print 15 + (-1*(((2500) + 11) % 7)) % 7
>>>17

Also when I run it in Java and C...
MrJackFrost
Posts: 18
Joined: May 25th, 2012, 8:41 pm

Re: Formula not returning expected result.

Post by MrJackFrost »

15 + (-1*(((2500) + 11) % 7)) % 7

...I think it is evaluating it like this (which is the correct way I believe)

1) 2500 + 11 = 2511
2) 2500 % 7 = 5
3) -1 * 5 = -5
4) -5 % 7 = 2
5) 15 + 2 = 17

...so possibly the math engine used for Rainmeter (if it is using a custom one) does not handle the order of operations correctly for this problem?
User avatar
Brian
Developer
Posts: 2688
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Formula not returning expected result.

Post by Brian »

Here is what I get in Visual Studio 10 (C++):
math.png
-Brian
You do not have the required permissions to view the files attached to this post.
User avatar
Brian
Developer
Posts: 2688
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Formula not returning expected result.

Post by Brian »

MrJackFrost wrote: 4) -5 % 7 = 2
This is false. -5 % 7 = 1, not 2.

-Brian
MrJackFrost
Posts: 18
Joined: May 25th, 2012, 8:41 pm

Re: Formula not returning expected result.

Post by MrJackFrost »

Brian wrote: This is false. -5 % 7 = 1, not 2.

-Brian
When you try to find, for example, 23 mod 3, you look at the largest multiple of 3 which is less than or equal to 23 (the key word here is less). That's 21, so 23 mod 3 = 23 - 21 = 2.

The same thing goes for negative numbers. The largest multiple of 7 that is less than -5 is -7. Just as you would subtracted 21 from 23 you subtract (-7) from (-5)

So --> -5 - (-7) = -5 + 7 = 2.
User avatar
Brian
Developer
Posts: 2688
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Formula not returning expected result.

Post by Brian »

MrJackFrost wrote:When you try to find, for example, 23 mod 3, you look at the largest multiple of 3 which is less than or equal to 23 (the key word here is less). That's 21, so 23 mod 3 = 23 - 21 = 2.

The same thing goes for negative numbers. The largest multiple of 7 that is less than -5 is -7. Just as you would subtracted 21 from 23 you subtract (-7) from (-5)

So --> -5 - (-7) = -5 + 7 = 2.
Thank you for the math lesson. I was basing my response on two factors. 1. Visual Studio's result. 2. Windows Calculator (try -5 mod 7, and you get -5).

After digging around the internet for a while, I found this: http://en.wikipedia.org/wiki/Modular_arithmetic#Remainders
in C++ negative number will be returned if the first argument is negative
So there you go.

-Brian
User avatar
Brian
Developer
Posts: 2688
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Formula not returning expected result.

Post by Brian »

Also, another thing to think about.

23 mod 3 = 2, right? So, we started counting from 3 to get to 21(closest multiple of 3 to 23). Then 23-21=2.
But in reality we didn't start from 3, we started from 0, and counted up in multiples of 3.

Likewise, -23 mod 3 = -2 because we don't start counting from 3, its 0. Instead of counting up, we count down to the nearest multiple (which is -21). So, -23-(-21) = -2.

(I just want to add, that this isn't necessarily mathematically correct.)

So lets apply this to our example.

5 mod 7 = 5, meaning there is no remainder.
-5 mod 7 = -5, there still is no remainder because you haven't reached a multiple of 7 (meaning -7 in this case).

I believe the thinking behind this is, if working with positive numbers, the result is positive (For example, 23 mod 3, we find the multiple of 3 that is closest to 23, which is 21. So we counted up from 3 until we hit 21. Result is 2).

In working with negative numbers, the result should be negative, so pretend the number is positive, then negate the result (For example, instead of using -23, use 23. 23 mod 3 = 2. Now negate the result. Answer is -2). Think of it as a mirror to the positive side of 0.

It sort of makes sense when you think about it, and remember, mathematics does not necessarily translate into computer code. Someone, somewhere had to make it work this way even if it doesn't work in the same way as regular mathematics work.

-Brian
MrJackFrost
Posts: 18
Joined: May 25th, 2012, 8:41 pm

Re: Formula not returning expected result.

Post by MrJackFrost »

Brian wrote:Also, another thing to think about.

23 mod 3 = 2, right? So, we started counting from 3 to get to 21(closest multiple of 3 to 23). Then 23-21=2.
But in reality we didn't start from 3, we started from 0, and counted up in multiples of 3.

Likewise, -23 mod 3 = -2 because we don't start counting from 3, its 0. Instead of counting up, we count down to the nearest multiple (which is -21). So, -23-(-21) = -2.

(I just want to add, that this isn't necessarily mathematically correct.)

So lets apply this to our example.

5 mod 7 = 5, meaning there is no remainder.
-5 mod 7 = -5, there still is no remainder because you haven't reached a multiple of 7 (meaning -7 in this case).

I believe the thinking behind this is, if working with positive numbers, the result is positive (For example, 23 mod 3, we find the multiple of 3 that is closest to 23, which is 21. So we counted up from 3 until we hit 21. Result is 2).

In working with negative numbers, the result should be negative, so pretend the number is positive, then negate the result (For example, instead of using -23, use 23. 23 mod 3 = 2. Now negate the result. Answer is -2). Think of it as a mirror to the positive side of 0.

It sort of makes sense when you think about it, and remember, mathematics does not necessarily translate into computer code. Someone, somewhere had to make it work this way even if it doesn't work in the same way as regular mathematics work.

-Brian
I'm sorry but I think that this is also incorrect. You need to find the closest multiple of three that is less than -23 (this goes for when you are modding both a positive or negative number). -21 > -23 so -21 would not be the number to use. The closest multiple of 3 that is less than -23 is -24 because -24 < -23. Then -23 - (-24) = -23 + 24 which = 1.