Support round(expression, precision), round(expression, precision, mode) · Issue #309 · FalkorDB/FalkorDB · GitHub
Skip to content

Support round(expression, precision), round(expression, precision, mode) #309

Description

@gkorland

Created by: @LiorKogan
Source: RedisGraph/RedisGraph#2614
Currently, RedisGraph does not support the following arguments combinations for round():

round(expression, precision)
round(expression, precision, mode)

Activity

  1. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor
  2. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @exitflynn
    i'd like to work on this issue!

  3. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @Khalid6468

    https://neo4j.com/docs/cypher-manual/current/functions/mathematical-numeric/#functions-round2

    @LiorKogan Please confirm if the given descriptions about different modes in this documentation are correct or not? I made changes as per this documentations definitions for all the modes.

  4. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @LiorKogan
    Reference implementation for HalfDown, HalfEven, and AwayFromZero rounding:

    // round towards closest integer, with half-values always being rounded down
    double RoundHalfDown(double f)
    {
        double int_part;
        double frac_part = modf(f, &int_part);
        
        if (frac_part <= -0.5) return int_part - 1.0;
        if (frac_part <=  0.5) return int_part      ;
                               return int_part + 1.0;
    }
    
    // round towards closest integer, with half-values being rounded to the even neighbor
    double RoundHalfEven(double f)
    {
        double int_part;
        double frac_part = modf(f, &int_part);
        
        if ((frac_part == -0.5) || (frac_part == 0.5))
            return round(f * 0.5) * 2.0;
        
        if (frac_part < -0.5) return int_part - 1.0;
        if (frac_part <  0.5) return int_part      ;
                              return int_part + 1.0;
    }
    
    // round away from zero
    double RoundAwayFromZero(double f)
    {
        double int_part;
        double frac_part = modf(f, &int_part);
        
        if (frac_part < 0) return int_part - 1.0;
        if (frac_part > 0) return int_part + 1.0;
                           return int_part      ;
    }
    
  5. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @LiorKogan
    @Khalid6468

    I think there are some problems with their convention. Their naming makes sense only for positive numbers.
    They also have a bug with HALF_DOWN and negative x.5 numbers.

    I would suggest the following:

    Keep compatibility with

    • CEILING
    • HALF_DOWN
    • HALF_EVEN
    • HALF_UP

    but the following are simply wrong:

    • 'DOWN' - Round towards zero. This is misleading. We should call it TOWARD_ZERO
    • 'UP' - Round away from zero. This is misleading. We should call it AWAY_FROM_ZERO
    • 'FLOOR' - Round towards zero. This is misleading. We should use the standard meaning of FLOOR
  6. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @Khalid6468
    Okay. Thanks for confirming.

  7. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @Khalid6468
    @LiorKogan Done with the implementation. Please have a look at the PR #2668. Here are the sample outputs for the round function:

    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.14)"
    1) 1) "round(3.14)"
    2) 1) 1) "3"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.14653, 3)"
    1) 1) "round(3.14653, 3)"
    2) 1) 1) "3.147"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1465, 3, 'HALF_UP')"
    1) 1) "round(3.1465, 3, 'HALF_UP')"
    2) 1) 1) "3.147"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1465, 3, 'HALF_DOWN')"
    1) 1) "round(3.1465, 3, 'HALF_DOWN')"
    2) 1) 1) "3.146"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1465, 3, 'HALF_EVEN')"
    1) 1) "round(3.1465, 3, 'HALF_EVEN')"
    2) 1) 1) "3.146"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1365, 3, 'HALF_EVEN')"
    1) 1) "round(3.1365, 3, 'HALF_EVEN')"
    2) 1) 1) "3.136"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1355, 3, 'HALF_EVEN')"
    1) 1) "round(3.1355, 3, 'HALF_EVEN')"
    2) 1) 1) "3.136"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1351, 3, 'CEILING')"
    1) 1) "round(3.1351, 3, 'CEILING')"
    2) 1) 1) "3.136"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1359, 3, 'CEILING')"
    1) 1) "round(3.1359, 3, 'CEILING')"
    2) 1) 1) "3.136"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1359, 3, 'FLOOR')"
    1) 1) "round(3.1359, 3, 'FLOOR')"
    2) 1) 1) "3.135"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1351, 3, 'FLOOR')"
    1) 1) "round(3.1351, 3, 'FLOOR')"
    2) 1) 1) "3.135"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1351, 3, 'AWAY_FROM_ZERO')"
    1) 1) "round(3.1351, 3, 'AWAY_FROM_ZERO')"
    2) 1) 1) "3.136"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(3.1351, 3, 'TOWARD_ZERO')"
    1) 1) "round(3.1351, 3, 'TOWARD_ZERO')"
    2) 1) 1) "3.135"
    127.0.0.1:6379> GRAPH.QUERY social "RETURN round(0.0, 3, 'TOWARD_ZERO')"
    1) 1) "round(0.0, 3, 'TOWARD_ZERO')"
    2) 1) 1) "0"

  8. gkorland commented on Sep 25, 2023

    @gkorland
    ContributorAuthor

    Origin comment by: @LiorKogan
    @Khalid6468 thank you. We'll review.
    Please make sure there are tests specifically for -1.5, -0.5, 0.5, and 1.5.
    Also for NaN, -INF, +INF, and large inputs (e.g., 1.7E308).

  9. added theissue type on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions