Can there be negative amounts in satoshis?

7 replies 195 views
Posts: 252 · Reputation: 12
#1Jul 12, 2023, 10:30 AM
I stumbled upon this article asking how many satoshis fit in a bitcoin. I could answer that without even thinking, but I got curious about why we measure Bitcoin in satoshis. So I peeked into the source code and found a pretty short snippet, only about 30 lines. But I got stuck on a couple of lines that seem to be causing the confusion.
3 Reply Quote Share
mr_apeNewbie
Posts: 401 · Reputation: 35
#2Jul 14, 2023, 05:35 PM
I think we can't have negative amounts. We just need checks to kick out any invalid entries. When you’re looking at signed numbers like int64_t, you gotta watch for negatives. Just checking against the max isn’t enough; even -1 is less than max. It’s rejected only if you look for signs.
4 Reply Quote Share
dave.forkMember
Posts: 375 · Reputation: 185
#3Jul 14, 2023, 08:43 PM
Right, in a 64-bit system, you can show anything from 1 satoshi to 21 quadrillion. But if we used Bitcoin instead, things would get messy with floating points. They only complicate stuff and aren’t needed here. Sure, they matter if we're talking about going beyond 21 million, but precision is key in currency.
3 Reply Quote Share
mr_apeNewbie
Posts: 401 · Reputation: 35
#4Jul 15, 2023, 01:43 AM
That article you shared was about timestamps and signed int32 issues, not the signed int64 for amounts. We don’t need to mix those topics up. Just saying.
2 Reply Quote Share
diamondhandsHero Member
Posts: 200 · Reputation: 2112
#5Jul 15, 2023, 01:49 AM
So let's say you can technically create a transaction with a negative fee. If you sign it right, you might see a negative value pop up. It’s just theoretical though; it doesn’t mean it’s valid everywhere. Bob can only claim it by adding his own inputs.
4 Reply Quote Share
Posts: 252 · Reputation: 12
#6Jul 15, 2023, 07:27 AM
You're saying no negatives are allowed, but the signed 64-bit type might allow negative checks? It’s all confusing, especially if the fee is negative. How does that even work? Does Alice's input just stay False until Bob joins in?
3 Reply Quote Share
wizard_2016Full Member
Posts: 127 · Reputation: 575
#7Jul 17, 2023, 11:52 AM
I think you’re kinda missing the point. CAmount is signed, so it can potentially hold negative values, but they aren’t valid everywhere. They just hint at possibilities. For example, if you check a wallet's balance history, sending bitcoins will show a negative change.
3 Reply Quote Share
mr_apeNewbie
Posts: 401 · Reputation: 35
#8Jul 17, 2023, 05:33 PM
But you still can’t have a negative amount in outputs. Only fees might go negative, and that’s calculated at a higher level for the UI. You can use unsigned integers to make sense of total inputs vs outputs. If you think of numbers as binaries, that clears up signed vs unsigned confusion.
0 Reply Quote Share

Related topics