iamriajul / adhan-dart

Adhan for Dart / Muslim Prayer Times Library. Now retrieving Prayer time in Dart easier than ever.
https://pub.dev/packages/adhan
MIT License
86 stars 41 forks source link

latitude more than 65: Error value should not be infinite or NaN #37

Open alhamri opened 2 years ago

alhamri commented 2 years ago

Describe the bug latitude values between (-67.5944046 and -90) and between (65.9277379 and 90) throw Unhandled exception!

To Reproduce minimum code that reproduce the error:

import 'package:adhan/adhan.dart';

void main() async {

  //latitude values between (-67.5944046 and -90) and between (65.9277379 and 90)
  //throw Unhandled exception!

  // var prayerTimes = PrayerTimes(Coordinates(-67.5944046, 120),
  //     DateComponents.from(DateTime.now()), CalculationMethod.umm_al_qura.getParameters());
  var prayerTimes = PrayerTimes(Coordinates(65.9277379, 120),
      DateComponents.from(DateTime.now()), CalculationMethod.umm_al_qura.getParameters());
  print(prayerTimes.fajr);
}

Expected behavior should output the actual time of the giving parameters.

but it throws an error:

C:/Flutter_SDK/flutter/bin/cache/dart-sdk/bin/dart.exe --enable-asserts "E:\Flutter_UI\hij_greg_temp - Copy\lib\main.dart"
Unhandled exception:
Invalid argument(s): value should not be infinite or NaN
#0      TimeComponents.fromDouble (package:adhan/src/data/time_components.dart:12:7)
#1      new PrayerTimes._ (package:adhan/src/prayer_times.dart:133:37)
#2      new PrayerTimes (package:adhan/src/prayer_times.dart:53:24)
#3      main (package:hij_greg_temp/main.dart:10:21)
#4      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Process finished with exit code 255

run flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, 3.1.0-9.0.pre, on Microsoft Windows [Version 10.0.19043.1766], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
[√] Chrome - develop for the web
[!] Visual Studio - develop for Windows (Visual Studio Enterprise 2019 16.11.1)
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK
[√] Android Studio (version 2021.1)
[√] Android Studio (version 4.1)
[√] Android Studio (version 4.2)
[√] VS Code (version 1.68.1)
[√] Connected device (4 available)
[√] HTTP Host Availability

! Doctor found issues in 1 category.

Environment(please complete the following information):

pk-acl commented 2 years ago

The error happens, because on the calculation of Astronomical.correctedHourAngle for sunrise with the coordinates given, the value passed to calculate degree is -1.0000000008014773. This is impossible to achieve hence error occurs.

alhamri commented 2 years ago

The error happens, because on the calculation of Astronomical.correctedHourAngle for sunrise with the coordinates given, the value passed to calculate degree is -1.0000000008014773. This is impossible to achieve hence error occurs.

yes the bug is there. I tried to solve it in the plugin by checking the value passed to acos function: if it is less than -1, assign -1 to the value if it is greater than 1, assign 1 to the value.

snippet code (before changing):

final term1 = sin(radians(h0)) -
        (sin(radians(coordinates.latitude)) * sin(radians(delta2)));
final term2 = cos(radians(coordinates.latitude)) * cos(radians(delta2));
final H0 = degrees(acos(term1 / term2));
final m = afterTransit ? m0 + (H0 / 360) : m0 - (H0 / 360);

snippet code (after changing):

final term1 = sin(radians(h0)) -
        (sin(radians(coordinates.latitude)) * sin(radians(delta2)));
final term2 = cos(radians(coordinates.latitude)) * cos(radians(delta2));
var y = term1 / term2;
if(y < -1.0) y = -1.0;
if(y > 1.0) y = 1.0;
final H0 = degrees(acos(y));
final m = afterTransit ? m0 + (H0 / 360) : m0 - (H0 / 360);

the exception is gone and it worked as expected, but i am not sure if the time given is correct or not?

JMApps commented 1 year ago

Have you found a solution? At the moment, the library throws an exception if the latitude is greater than 73 degrees

iamriajul commented 11 months ago

final term1 = sin(radians(h0)) - (sin(radians(coordinates.latitude)) sin(radians(delta2))); final term2 = cos(radians(coordinates.latitude)) cos(radians(delta2)); var y = term1 / term2; if(y < -1.0) y = -1.0; if(y > 1.0) y = 1.0; final H0 = degrees(acos(y)); final m = afterTransit ? m0 + (H0 / 360) : m0 - (H0 / 360);

Changes are causing tests to fail!

image