Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Number IsInt, IsUint, IsFloat, IsNan #155

Closed
gavv opened this issue Nov 23, 2022 · 11 comments
Closed

Implement Number IsInt, IsUint, IsFloat, IsNan #155

gavv opened this issue Nov 23, 2022 · 11 comments
Assignees
Labels
feature New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Milestone

Comments

@gavv
Copy link
Owner

gavv commented Nov 23, 2022

Implement new Number assertions:

  • IsInt - succeeds if number is signed or unsigned integer (does not have fractional part)
  • IsUint - succeeds if number is unsigned integer (does not have fractional part and is >= 0)
  • IsFloat - succeeds if number is not integer (have fractional part, is opposite to IsInt)
  • IsNan - succeeds if number is NaN

Each of them, except IsNan, should have optional argument bits which defines maximum allowed bitness for the number, e.g. IsInt(32) means that the number should fit in 32-bit signed integer. See String.AsNumber for en example of optional argument.

For every assertion, also add negative version (NotInt, NotUint, etc).

Each assertion should have documentation comment with example and be covered by unit tests. Don't forget to cover corner cases like NaN, inf, etc.

@gavv gavv added feature New feature or request help wanted Contributions are welcome good first issue Good for newcomers labels Nov 23, 2022
@gavv gavv changed the title Implement Number IsInt, IsUint, IsFloat Implement Number IsInt, IsUint, IsFloat, IsNan Nov 23, 2022
@deepto98
Copy link

I'd like to give this a shot. Can I pick this up?

@gavv
Copy link
Owner Author

gavv commented Dec 18, 2022

Sure, thanks!

@gavv
Copy link
Owner Author

gavv commented Jan 13, 2023

@deepto98 Hi, do you still have plans on this?

@gavv
Copy link
Owner Author

gavv commented Feb 1, 2023

Unassigning, so that someone could pick this up.

@gavv gavv mentioned this issue Feb 8, 2023
7 tasks
@fahimbagar
Copy link
Contributor

@gavv, before I continue, could you check the WIP PR and see whether the solution is fine?

I haven't finished IsFloat and IsNaN, need to clarify something

  1. The current solution for integer is to convert them to string first and then use ParseInt. Is this correct?
    • I am wondering why the bit is too flexible. I mean something like this could easily check whether it's integer math.Mod(floatValue, 1.0) == 0. Or maybe limit it to 32 and 64. Wdyt?
  2. strconv.ParseFloat recognizes NaN and Inf. Should it be failed on IsFloat case?

ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity" as their respective special floating point values. It ignores cases when matching.
https://pkg.go.dev/strconv#ParseFloat

@gavv
Copy link
Owner Author

gavv commented Feb 8, 2023

@fahimbagar thanks for taking up this. Will reply shortly.

@gavv
Copy link
Owner Author

gavv commented Feb 8, 2023

Hi, here are some thoughts.

  1. Using fmt.Sprintf is an interesting idea, and probably is correct, but instead I'd suggest to use math.Modf and check that the value belongs to desired range (e.g. [0; 2^bits]). Rationale:

    • Are we sure that Sprintf + ParseInt wont loose precision? Are we sure that it wont change in future? Even if it doesn't lose precision currently, why bother, if can just use Modf.

    • There is a task in progress (Migrate Number from float64 to big.Float #191) that switches Number from float64 to big.Float. The approach with Modf will be easier to port to big.Float.

  2. I think there is no need to limit bit width to 32/64. Yes, other values are rarely needed, but I see no problem with accepting them. Maybe someone will need to check that integers are in 24 bit range, who knows.

  3. Yes, I think NaN and Inf should be accepted by IsFloat. They are correct float values, same as signed zero for example.

@fahimbagar
Copy link
Contributor

Noted, thanks for the advice, @gavv. I'll adjust the PR then.

@fahimbagar
Copy link
Contributor

Hey @gavv, I'm stuck with bitness size in IsFloat & NotFloat. 😅
How to measure the maximum bitness of float other than 32-bit and 64-bit? I don't have quite any experience in this.

Maybe I should wait for #191. Otherwise, this PR can be reviewed.

@gavv
Copy link
Owner Author

gavv commented Feb 8, 2023

I think for floats it's ok to limit allowed values to 32 and 64, at least for now. After implementing big.Float we can revise it.

No need to wait for #191, I guess it will take time until it's ready.

@gavv
Copy link
Owner Author

gavv commented Feb 22, 2023

Landed!

@gavv gavv closed this as completed Feb 22, 2023
@gavv gavv added this to the v2 milestone Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Projects
None yet
Development

No branches or pull requests

3 participants