GithubHelp home page GithubHelp logo

Comments (6)

konsumlamm avatar konsumlamm commented on June 15, 2024 1

This issue should be reopened, toSignedInt only provides conversion to signed integers. I'd also argue that it should be renamed to toInt and also support unsigned integers (rather than adding a new toUnsignedInt function).

from bigints.

konsumlamm avatar konsumlamm commented on June 15, 2024

The only workaround yet is to use the keyword cast[int]. But this does not always do what we want.

That never does what you want. I suggest you read the documentation for cast, it merely reinterprets the underlying bits.

I generally agree that functions to convert a BigInt to some integer types are useful, but how should we handle the case of the BigInt being out of range? I see several possibilities:

  • raise an exception
  • return an Option
  • return the lowest/highest value (especially for floats, where we have Inf/-Inf)
  • cut off the higher bits (i.e. return x mod 2^n where n is the number of bits of the resut)

EDIT: Added a 4th option.

from bigints.

narimiran avatar narimiran commented on June 15, 2024

@Araq: you might be interested in this discussion, i.e. give your opinion on how to best proceed with this.

from bigints.

dlesnoff avatar dlesnoff commented on June 15, 2024

We can not return the lowest/highest value for type conversions to integers as a BigInt can be any value that an int can contain.
The first solution forces the user to wrap any potentially dangerous cast in a try .. except .. finally clause.
I have not used Option in my programs yet, if I understand the user will have to check (everytime ?) whether the Option contains a value ?

from bigints.

Araq avatar Araq commented on June 15, 2024

IMO the prototype should be:

proc toFixedInt*[T](b: BigInt): Option[T]

with the hopefully obvious semantics.

from bigints.

Araq avatar Araq commented on June 15, 2024

I also really need pred and succ. Implementation can be as simple as:

proc pred*(a: BigInt; def = 1): BigInt =
  a - initBigInt(def)

proc succ*(a: BigInt; def = 1): BigInt =
  a + initBigInt(def)

from bigints.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.