cot(0)/coth(0) return Infinity while SQLite's own division returns NULL

Open Beginner friendly
#1,515 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, java, sqlite
Domain
databases

Research direction

Read the cot/coth wrappers in src/main/ext/extension-functions.c and see where the result is pushed with sqlite3_result_double(). Change them so a zero denominator or non-finite result becomes sqlite3_result_null() instead. Verify by running the JDBC snippet in the issue against a rebuilt extension: cot(0) and coth(0) should print null, matching 1.0/tan(0) and 1.0/tanh(0).

Written by the indexing model from the issue text.

Description

triage

Describe the bug

cot(0) and coth(0) return IEEE Infinity, while the equivalent expression written with SQLite's own operators (1.0/tan(0)) returns NULL, because SQLite defines division by zero as NULL. The same mathematical operation therefore yields two different results inside one database connection, depending on whether it is spelled as a function call or as an expression.

To Reproduce

import java.sql.*;

public class Repro {
  public static void main(String[] args) throws Exception {
    try (Connection c = DriverManager.getConnection("jdbc:sqlite::memory:");
         Statement s = c.createStatement()) {
      String[] q = {
        "SELECT cot(0), 1.0/tan(0)",
        "SELECT coth(0), 1.0/tanh(0)"
      };
      for (String x : q) {
        try (ResultSet r = s.executeQuery(x)) {
          r.next();
          System.out.println(x + "  => " + r.getObject(1) + " | " + r.getObject(2));
        }
      }
    }
  }
}

Expected behavior

cot(0) and coth(0) should return NULL, matching SQLite's division-by-zero semantics (1.0/tan(0) is already NULL), instead of exposing an infinity value that SQLite itself never produces from arithmetic.

Logs

SELECT cot(0), 1.0/tan(0)    => Infinity | null
SELECT coth(0), 1.0/tanh(0)  => Infinity | null

Environment (please complete the following information):

  • OS: macOS 15.6.1
  • CPU architecture: arm64
  • sqlite-jdbc version: 3.54.1.0

Additional context

Root cause in src/main/ext/extension-functions.c: cot/coth are plain C wrappers over 1.0/tan(x) and 1.0/tanh(x); C division yields Inf (or raises a domain error caught by errno), and the value is pushed straight back with sqlite3_result_double(). SQLite never sees a division operator, so its own "divide by zero is NULL" rule is bypassed.

Suggested fix: explicitly convert a non-finite result to NULL, or a zero denominator to NULL, so the function agrees with the arithmetic semantics of the database it runs in.

Found by PST while testing built-in functions locally.

Dominant language
Java
Stars
3.3k
Forks
676
Avg merge
1d 15h
Merged PRs (30d)
18

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from xerial/sqlite-jdbc

All issues in xerial/sqlite-jdbc

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.