A question about modulo calculations

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

A question about modulo calculations

Post by tiny »

If I compute the calculation -6 mod 7 in Excel, the result is 1. In FC7 always -6 comes as result.
There are 2 entries in the FC7 Wiki:

float = fmod (float, float) MOD function for floating point numbers
and
fmod (x, y) Floating point modulus (remainder of x divided by y)

f1 = -6.00
f2 = 7.00
f3 = f1 MOD f2
f4 = fmod (f1, f2)

Both tried and both come the result -6 instead of 1.

Can someone help me with this problem?

Thank's
Christina

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: A question about modulo calculations

Post by mnf »

Hi Christina,

The result is correct -6 % (mod) 7 is the remainder after -6 is integer divided by 7 (goes 0 times with -6 remainder) - the first number is divided by the second.

So 6 mod 7 result 6 corresponds in syntax to 6 / 7 = 0. (In C 6 % 7)

Hope that makes sense....

Martin

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: A question about modulo calculations

Post by tiny »

Thanks for the answer. There should be a correction in Excel, because the result is -6 mod 7 = 1.
I am looking for a formula to calculate the day of the week including the leap years and in a formula suggestion there was exactly this case.
I'm looking for a working formula, but thanks again for the explanation!

Christina

mnf
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: A question about modulo calculations

Post by mnf »

You probably googled it already but Wikipedia has a good resource
https://en.m.wikipedia.org/wiki/Determi ... f_the_week
Sorry that is the address for the mobile version.
The matrix forum's security certificate seems to have expired, lots of error messages luring me back to safety?

Martin

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: A question about modulo calculations

Post by tiny »

I have the german wikipedia page with the same Gaussian formulas and monthly corrections. After I use 1 for Sunday and 7 for Saturday, there is still a conversion over a byte array. This is pretty good. Here is a first (hopefully) working example.

Thanks again!
Christina
DayOfWeek.fcfx
(13.52 KiB) Downloaded 271 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: A question about modulo calculations

Post by medelec35 »

Hi Christina,
I have attached a stripped down flowchart that calculates the day of the week, if it's currently GMT or BST and the dates in march and October when the clocks change:
DOTW1.png
(54.61 KiB) Downloaded 2921 times
Uses Bytes to save memory compared to floats.

Martin
Attachments
DOTW.fcfx
(20.39 KiB) Downloaded 183 times
Martin

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: A question about modulo calculations

Post by tiny »

Hello Martin,
thanks for the hint and the flowchart! I think probably too complicated!
I'll work through your chart.

Thank you and best regards
Christina

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: A question about modulo calculations

Post by medelec35 »

Hi Christina,
You're welcome.
It was more complex because I changed the formula and added a loop, so it can calculate the dates when the clock change in the UK and automatically adjust for the correct time.

I have removed that function, so its now in the simplest form.

Martin
Attachments
Simplified Day Of Week.fcfx
(15.93 KiB) Downloaded 198 times
Martin

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: A question about modulo calculations

Post by tiny »

Hello Martin,
my problem is that I'm using a DS3231 that spends the day of the week, but postponed by up to 2 days (Tuesday instead of Sunday), constant throughout the year. At the turn of the year, the deviation was reduced by one day. I've included a routine that corrects the deviation, but that's not the solution I want to use. Another DS3231 module showed similar symptoms, with a different deviation. System error? No idea.
After switching to FC 7.3 the DS3231 macro did not work, I now use the internal RTC function. Goes well, but with the said deviation.
That's why I just wanted to calculate the day of the week myself. Your suggestion is simple but ingenious! Thank you very much for that!

Christina

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: A question about modulo calculations

Post by medelec35 »

I created a project that used the PC time and date to set the time on a DS3231 via USB.
So I can help you get the 3231 to display correct day if you get stuck.

Martin
Martin

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: A question about modulo calculations

Post by tiny »

Hi Martin,

thanks, the time and date feature of the DS3231 is sufficiently accurate and with the ability to calculate the day of the week itself, it is also independent of any unsatisfactory aspects of a clock chip. That's enough for me. I have sufficient space for program and data, I use the ECIO40P16. Great thing!
The program is used for a pool control with heating and control of the water quality (temperature, pH value). It was the last two years already in use, but I just had to perform the daily correction. That is now (hopefully) gone!

Thanks for the many and quick help, great forum!

Christina

Post Reply