prajwalkpatil / VedicDateTime

VedicDateTime: An R package to implement Vedic calendar system
https://cran.r-project.org/package=VedicDateTime
GNU General Public License v3.0
6 stars 3 forks source link

Wrong yoga computation, resulting in out of bounds array access in get_yoga_name function #14

Closed sanivella closed 1 year ago

sanivella commented 1 year ago

Thanks a lot for the amazing library.

While I was trying to use get_yoga_name, I've observed that returned name contains 'NA'. So I have tried observing code execution using breakpoints to see what might be happening within get_yoga_name. Following are my observations

Sample 1

get_yoga_name(gregorian_to_jd(05,07,2023), c(16.99, 81.79, +5.5)) [1] "Vaidhriti till 7:47:38 & NA till 27:48:33"

Sample 2

yoga(gregorian_to_jd(05,07,2023), c(16.99, 81.79, +5.5)) [1] 27 7 47 38 28 27 48 33

I could see that in the for loop, during 2nd iteration variable j value is set to 5 resulting in accessing 5th element in the yoga_ array i.e. 28 in this example, which in turn produces 'NA' value due to out of bound access of yogas array.

I suspect there is an issue in yoga function which wrongly computed value 28 for 5th element in yoga_ array.

get_yoga_name <- function(jd,place){
  yoga_ = yoga(jd,place)
  size = length(yoga_)
  size = size / 4
  j <- 1
  yoga_name <- ""
  for(i in 1:size){
    #* 'NA' caused by following line of code during 2nd iteration
    yoga_name <- paste(yoga_name,yogas[yoga_[j]]," till",sep = "")
    yoga_name <- paste(yoga_name,paste(yoga_[j+1], yoga_[j+2], yoga_[j+3], sep = ":"))
    if(size > 1 && i == 1){
      yoga_name <- paste(yoga_name,"& ")
    }
    j <- 5
  }
  return (yoga_name)
}
prajwalkpatil commented 1 year ago

Fixed it - #15 & #17

Thanks a lot!