Skip to content

2026 PKUCourseHW5: more precise log for Fcoef::create#7126

Closed
kevin0x3f wants to merge 1 commit intodeepmodeling:developfrom
kevin0x3f:my-feature-branch
Closed

2026 PKUCourseHW5: more precise log for Fcoef::create#7126
kevin0x3f wants to merge 1 commit intodeepmodeling:developfrom
kevin0x3f:my-feature-branch

Conversation

@kevin0x3f
Copy link

Reminder

  • Have you linked an issue with this pull request?
  • Have you added adequate unit tests and/or case tests for your pull request?
  • Have you noticed possible changes of behavior below or in the linked issue?
  • Have you explained the changes of codes in core modules of ESolver, HSolver, ElecState, Hamilt, Operator or Psi? (ignore if not applicable)

Linked Issue

Fix #...

Unit Tests and/or Case Tests for my changes

  • A unit test is added for each new feature or bug fix.

What's changed?

  • Example: My changes might affect the performance of the application under certain conditions, and I have tested the impact on various scenarios...

Any changes of core modules? (ignore if not applicable)

  • Example: I have added a new virtual function in the esolver base class in order to ...

Copilot AI review requested due to automatic review settings March 24, 2026 05:35
@kevin0x3f kevin0x3f changed the title more precise log for Fcoef::create 2026 PKUCourseHW5: more precise log for Fcoef::create Mar 24, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the diagnostic output from Fcoef::create by making the log messages more precise and adding extra context when invalid inputs are detected.

Changes:

  • Prefixes error logs with Fcoef::create for clearer source attribution.
  • Adds a “possible integer overflow” diagnostic that logs i1, i2, i3, and their computed product.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 31 to 34
if (!(i1 * i2 * i3 > 0)){
std::cout << "i1*i2*i3 must be positive! i1*i2*i3 is " << i1 * i2 * i3 << std::endl;
unsigned long long product = static_cast<unsigned long long>(i1) * static_cast<unsigned long long>(i2) * static_cast<unsigned long long>(i3);
std::cout << "Fcoef::create: possible integer overflow in product i1*i2*i3! i1 is " << i1 << ", i2 is " << i2 << ", i3 is " << i3 << ", product is " << product << std::endl;
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition i1 * i2 * i3 > 0 can itself trigger signed integer overflow (undefined behavior) before you get to the diagnostic, which defeats the purpose of detecting overflow. Also, casting negative int values to unsigned long long will produce very large values, so the printed product can be misleading when the real problem is simply a negative input. A safer approach is to check i1/i2/i3 positivity without multiplying, and only compute a product using a wide signed type (e.g., long long/__int128) after confirming all are positive (or perform overflow-checked multiplication).

Copilot uses AI. Check for mistakes.
Comment on lines +30 to +42
std::cout << "Fcoef::create: not allowed!" << std::endl;
if (!(i1 * i2 * i3 > 0)){
std::cout << "i1*i2*i3 must be positive! i1*i2*i3 is " << i1 * i2 * i3 << std::endl;
unsigned long long product = static_cast<unsigned long long>(i1) * static_cast<unsigned long long>(i2) * static_cast<unsigned long long>(i3);
std::cout << "Fcoef::create: possible integer overflow in product i1*i2*i3! i1 is " << i1 << ", i2 is " << i2 << ", i3 is " << i3 << ", product is " << product << std::endl;
}
if (!(i1 > 0)){
std::cout << "i1 must be positive! i1 is " << i1 << std::endl;
std::cout << "Fcoef::create: i1 must be positive! i1 is " << i1 << std::endl;
}
if (!(i2 > 0)){
std::cout << "i2 must be positive! i2 is " << i2 << std::endl;
std::cout << "Fcoef::create: i2 must be positive! i2 is " << i2 << std::endl;
}
if (!(i3 > 0)){
std::cout << "i3 must be positive! i3 is " << i3 << std::endl;
std::cout << "Fcoef::create: i3 must be positive! i3 is " << i3 << std::endl;
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are error diagnostics; using std::cerr (instead of std::cout) is generally more appropriate and makes it easier to separate normal output from error output in logs/pipes. Consider switching these streams (and keeping the prefixing you added).

Copilot uses AI. Check for mistakes.
@mohanchen
Copy link
Collaborator

Nice try! Thanks for your contribution!

@mohanchen mohanchen closed this Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants