Python Forum
math formula does not give the same result as bash script [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
math formula does not give the same result as bash script [SOLVED]
#4
(Apr-02-2023, 11:38 AM)deanhystad Wrote: This should divide by 12, not 4.
d = ((367 * (month-2-12*a)) / 4)
The other differences are caused by Python doing floating point math and bash doing integer math. The first pass at fixing your equations I tried this:
a = ((month-14) // 12)
b = ((year+4900+a) // 100)
c = ((1461*(year+4800+a)) // 4)
d = ((367 * (month-2-12*a)) // 12)
This was a little off. a was -1 instead of 0. When doing integer division, // does floor division, not truncating. Use int() to truncate.

Second pass.
a = int((month-14) / 12)
b = int((year+4900+a) / 100)
c = int((1461*(year+4800+a)) / 4)
d = int((367 * (month-2-12*a)) / 12)
I see no reason for adding 1 to the day. This is my version of your converter. I hard coded date to match your example.
from datetime import datetime


now = datetime.strptime("3 4 2023", "%d %m %Y")
# Remove comment to convert current date
# now = datetime.now()
day = now.day
month = now.month
year = now.year
print(day, month, year)

a = int((month-14) / 12)
b = int((year+4900+a) / 100)
c = int((1461*(year+4800+a)) / 4)
d = int((367 * (month-2-12*a)) / 12)
print(a, b, c, d)
Output:
3 4 2023 0 69 2492100 61

Ahh thank you. Didn't thought of that. But it worked :)
Reply


Messages In This Thread
RE: math formula does not give the same result as bash script - by AlphaInc - Apr-02-2023, 07:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 604 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 1,006 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,821 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  math.log versus math.log10 stevendaprano 10 2,603 May-23-2022, 08:59 PM
Last Post: jefsummers
  Call a bash script from within a Python programme Pedroski55 6 2,573 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  {SOLVED]Help getting formula value in Excel to a csv Pedroski55 1 2,112 Sep-20-2021, 12:19 AM
Last Post: Pedroski55
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,765 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,895 May-18-2021, 06:42 PM
Last Post: Skaperen
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,911 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,646 Jan-16-2021, 10:20 PM
Last Post: Miletkir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020