Skip to content

proposal: intmath: new package #51563

Closed
Closed
@perillo

Description

@perillo

In same cases, like pagination, it is necessary to do:

pages = int(math.Ceiling(float64(items) / float64(itemsPerPage);

According to http://www.cs.nott.ac.uk/~rcb/G51MPC/slides/NumberLogic.pdf, the code can be optimized to avoid using floating point support:

pages = (items + itemsPerPage - 1) / itemsPerPage

According to https://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division, a simplified version exists that avoid a possible overflow:

pages = (items - 1)  / itemsPerPage + 1

I have not verified these algorithms. However it should be evident that it is not a trivial problem.

In addition to CeilDiv there are also the CeilMod, FloorDiv, FloorMod and Abs functions.

Finally I also propose to add functions so that integer overflow is reported with an error, like AbsExact, AddExact, DivideExact, MultiplyExact, NegateExact, SubtractExact, CeilDivExact, FloorDivExact.

These functions accept a signed integer (int) as arguments.

References

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions