Well, the correct result would be 35,514…, it’s debatable how to round this.
You should probably avoid the intermediate divisions and do a single division at the end:
a = 5 * (numDay + self.quantieme() + 5)
b = 7 * numDay
numerator = a - b
wn = numerator / 35
This would round down. If you want to round up, then do:
Could you clarify what you’re trying to calculate?
It seems that in the first post, you were trying to calculate the number of the week (from 1 to 53), given the number of the day (from 1 to 365). But what is the self.quantieme() and how does it differ from numDay?
Perhaps this is what you wanted:
fn week(day: u16) u16{
// change from 1 to 0-based
const d = day - 1;
//Rounded up integer division
const numerator = d + 6;
const week = @divFloor(numerator, 7);
// Change it back to 1-based
return w + 1;
}
With day = 245, it gives the correct week number 36.
I think it would be helpful if you could provide a reference for what you’re trying to calculate (as @LucasSantos91 suggested) and perhaps some tests showing the desired result and what you’re actually getting.
However, I did a quick search for “Zeller”, and I think this is what you’re trying to use: Zeller's congruence - Wikipedia The issue I see is that you’re using the wrong formula: the one with + 5J is under the “implementations in software” section with the note:
Unfortunately, in the truncating way most computer languages implement the remainder function, −2 mod 7 returns a result of −2. So, to implement Zeller’s congruence on a computer, the formulas should be altered slightly to ensure a positive numerator.
But you’re using @mod in your implementation, which is the mathematical modulo operator, so you should actually be using the original - 2J formula (or use @rem instead).Edit: actually, on second thought, this shouldn’t matter, since the difference 7J between the formulas is congruent to 0 mod 7… but I think the distinction between @mod and @rem is still worth bearing in mind.
@mod - mathematical modulo operation (example from the langref: @mod(-5, 3) == 1)
@rem - “traditional” C programmer’s % operation (example from the langref: @rem(-5, 3) == -2)
// Calcul standard du numéro de semaine Standard calculation of week number
var wn = (nd + self.quantieme() + 5) / 7 - (nd / 5);
Hello, first of all thank you for taking the time to answer, indeed, it is a formula that can be found either at the university, or on ChatGPT, or in the date processing for the 52 / 53 week or week number.
I’ve chosen the Representation of Dates and Times standard (referred to as ISO 8601:1988) standard because I’ve never had a problem constructing a date with the timestamp, it’s to solve a problem like making a calendar that can be used in business, I’ve done it on AS400 … Well, even though I’m retired, I’m trying to reproduce an environment based on the As400 mode.
this model where everything is f32 does not respond correctly to find week zero
but I use this model once I have found week zero
@trunc((numDay + Q + 5) / 7 - (numDay / 5));
To recover 52 … because it only gives me the 52 week the formula is designed to calculate standard weeks.
but when I check sometimes I have drifts with the basic formula var wn = (nd + self.quantieme() + 5) / 7 - (nd / 5);
to recover week zero where everything is integer , I know I’m not simple …