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]
#1
Hello everybody,

I was working on a script which gets the current date of the islamic calendar. I know there is a pip-module which converts a Georgian date to islamic date but it's only defined until 2077. I found there is a script on GitHub (https://github.com/gojigeje/hijri.sh) which gets the current date and it works far later than 2077.

The problem I have is that the bash script outputs the different results than when I "converted" the bash script to python. For example, this is the bash script for the current day (2 April 2023):

adjust=1

date=$(date +%d -d "$adjust day" | sed 's/^0*//')
month=$(date +%m | sed 's/^0*//')
year=$(date +%Y)

a=$(( $(( $month-14)) / 12 ))
b=$(( $(( $year+4900+$a )) / 100 ))
c=$(( $(( 1461 * $(( $year+4800+$a )) )) / 4 ))
d=$(( $(( 367 * $(( $month-2-12*$a )) )) / 12 ))
This is the output for the bash script when I echo the results:

3
4
2023
0
69
2492100
61
This is my python script:

#!/usr/bin/env Python3

#Imports
from datetime import datetime
import sys

#Get Date
day = datetime.now().day + 1 #Why adjust one date
month = datetime.now().month
year = datetime.now().year

#Formula
a = ((month-14) / 12)
b = ((year+4900+math.trunc(a)) / 100)
c = ((1461*(year+4800+a)) / 4)
d = ((367 * (month-2-12*a)) / 4)
And this is the output I get for day, month and year and a, b, c and d:

3
4
2023
-0.8333333333333334
69.23
2491796.375
1101.0
Does anybody why the results of my python script differ so much to the ones from the bash script?
Reply
#2
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
Reply
#3
Maybe (for my benefit, least ways) you should explain your formulas.

Also, you have four objects (a,b,c and d) but you have seven lines of output, so it's not altogether clear which object relates to which output.

A good example of the way to show that, would be print(f"a = {a}")

About the above example (which seem to be line four of your output) right now we're in month 4, so 4-14 = -10 and -10 / 12 = -0.8333333333333334
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 417 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 934 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,572 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  math.log versus math.log10 stevendaprano 10 2,426 May-23-2022, 08:59 PM
Last Post: jefsummers
  Call a bash script from within a Python programme Pedroski55 6 2,474 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  {SOLVED]Help getting formula value in Excel to a csv Pedroski55 1 2,035 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,691 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,793 May-18-2021, 06:42 PM
Last Post: Skaperen
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,782 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  [SOLVED] Requiring help running an old Python script (non Python savvy user) Miletkir 13 5,453 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