Skip to content

Add a condition for parsing zero from string when not denominated.#3346

Merged
apoelstra merged 1 commit into
rust-bitcoin:masterfrom
yancyribbens:zero-denom
Sep 13, 2024
Merged

Add a condition for parsing zero from string when not denominated.#3346
apoelstra merged 1 commit into
rust-bitcoin:masterfrom
yancyribbens:zero-denom

Conversation

@yancyribbens

Copy link
Copy Markdown
Contributor

closes #3307

@github-actions github-actions Bot added the C-units PRs modifying the units crate label Sep 11, 2024
@coveralls

coveralls commented Sep 11, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 10833023984

Details

  • 30 of 31 (96.77%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 82.801%

Changes Missing Coverage Covered Lines Changed/Added Lines %
units/src/amount.rs 30 31 96.77%
Totals Coverage Status
Change from base Build 10815518805: 0.02%
Covered Lines: 18944
Relevant Lines: 22879

💛 - Coveralls

Comment thread units/src/amount.rs Outdated

@Kixunil Kixunil left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do it as I recommended in the issue (try to parse it with denomination and if it fails also try the usual parsing function and check for zero). The way you wrote it makes the code possibly inconsistent and I suspect it'd break for inputs like 0.00000000000000001.

@yancyribbens

Copy link
Copy Markdown
Contributor Author

Please do it as I recommended in the issue (try to parse it with denomination and if it fails also try the usual parsing function and check for zero). The way you wrote it makes the code possibly inconsistent and I suspect it'd break for inputs like 0.00000000000000001.

I'll take a look again. It seemed overly complicated to do it that way, but maybe I overlooked something.

@yancyribbens yancyribbens force-pushed the zero-denom branch 2 times, most recently from 5b7e74d to fb92ce7 Compare September 11, 2024 15:30
@yancyribbens

Copy link
Copy Markdown
Contributor Author

@Kixunil I've updated it as you suggested. It's a bit more verbose but I understand wanting consistency. I'm kinda surprised not just using standard float parsing under the hood, although I imagine there is a reason..

@yancyribbens yancyribbens force-pushed the zero-denom branch 2 times, most recently from 699b055 to 4ff3f4b Compare September 11, 2024 16:49
@yancyribbens

Copy link
Copy Markdown
Contributor Author

It looks like alloc is now required unless I'm missing something.

@Kixunil

Kixunil commented Sep 11, 2024

Copy link
Copy Markdown
Collaborator

I'm kinda surprised not just using standard float parsing under the hood, although I imagine there is a reason..

The reason is floats are absolutely wrong for financial applications because they are not precise enough. There's a ton about this on the Internet, I'm sure you'll find more information easily if you need it.

alloc shouldn't be needed but it might if we have input string that is not InputString. I'll look into it later, I can only do surface-level stuff right now.

@yancyribbens

Copy link
Copy Markdown
Contributor Author

The reason is floats are absolutely wrong for financial applications because they are not precise enough. There's a ton about this on the Internet, I'm sure you'll find more information easily if you need it.

Hmm that's interesting. I read that financial applications usually use decimal notation with fixed precision vs a float where the precision "floats". I take it that means the rust library rust_decimal would be comparable to Amounts numeric representation.

alloc shouldn't be needed but it might if we have input string that is not InputString. I'll look into it later, I can only do surface-level stuff right now.

The source string "0" has " sat" concatenated to the end so that it doesn't fail to parse. somehow there needs to be an allocation so that the source string can resize to hold the denomination which is why I think it needs alloc, unless Im mistaken.

Comment thread units/src/amount.rs Outdated
Comment thread units/src/amount.rs Outdated
Comment thread units/src/amount.rs Outdated
@Kixunil

Kixunil commented Sep 12, 2024

Copy link
Copy Markdown
Collaborator

Also this needs documenting how FromStr differs from from_str_with_denomination. I think the difference makes sense. I've briefly considered deprecating from_str_with_denomination but it looks like it might be useful to keep it.

@yancyribbens

Copy link
Copy Markdown
Contributor Author

Also this needs documenting how FromStr differs from from_str_with_denomination. I think the difference makes sense. I've briefly considered deprecating from_str_with_denomination but it looks like it might be useful to keep it.

I would have thought there be a compile error about from_str not being documented. Anyway, now the difference I see is just that from_str doesn't imply denomination is required, however the only case where that makes sense is with zero. Also its counter intuitive the way the naming is done using preposition like with to mean that the denomination is provided in the string and not as formal argument. For example, from_str_in takes denom as an argument, so I would have thought this would be named .._with_denom. I think from_str_with_denomination is more precise as from_str_include_denomination.

@yancyribbens

yancyribbens commented Sep 12, 2024

Copy link
Copy Markdown
Contributor Author

/// If you want to parse only the amount without the denomination, use [Self::from_str_in].

On topic, if you want to parse only the amount given a denomination parameter, or something like that is better imo

@Kixunil Kixunil left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 894f82e

Comment thread units/src/amount.rs
let _a = SignedAmount::from_str("0").unwrap();
let _a = SignedAmount::from_str("0.0").unwrap();
let _a = SignedAmount::from_str("00.0").unwrap();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test confirming that 0.00 parses would be nice too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can just do in another comment since this one is already acked. This is also something that could just be part of a proptest given arbitrary dot placement which is probably more effective.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea turning it to proptest, but I'd like to see also various lengths not just dot placement being changed.

Comment thread units/src/amount.rs

@tcharding tcharding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 894f82e

@apoelstra apoelstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 894f82e; successfully ran local tests.

@apoelstra apoelstra merged commit 7360c3c into rust-bitcoin:master Sep 13, 2024
apoelstra added a commit that referenced this pull request Sep 18, 2024
f5cae1c Comment from_str methods (yancy)

Pull request description:

  Follow up from #3346

ACKs for top commit:
  tcharding:
    ACK f5cae1c
  apoelstra:
    ACK f5cae1c successfully ran local tests

Tree-SHA512: 2b95381e5281754e2b3a49aa8dfaac5742c244970fb54f68024dc23b61a74955ae95b9a0e7ae848095ac0686df5faf93faf7de3371b2f341b108cc10e5d4a9cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-units PRs modifying the units crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

from_str zero needs no denomination

5 participants