Page MenuHomePhabricator

Add default construct, and several ops, to Amount class
ClosedPublic

Authored by schancel on Sep 20 2017, 12:19.

Details

Summary
  • Add default constructor necessary for use with STL containers.
  • Add division and modulus operators which are used routinely.
Test Plan

make VERBOSE=1 check

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

src/amount.h
85 ↗(On Diff #1392)

The return value seems off. However, given that T has no units associated with it (or unknown units), I think this is correct.

deadalnix requested changes to this revision.Sep 21 2017, 19:23
deadalnix added inline comments.
src/amount.h
14 ↗(On Diff #1392)

This guy should have been removed.

73 ↗(On Diff #1392)

Why double ? I don't think we should use floating point implicitely. Also do not use C style cast, use C++ constructors.

75 ↗(On Diff #1392)

I think you should just use the delete trick.

77 ↗(On Diff #1392)

Division is not commutative, so you can make this a member function rather than a friend one.

80 ↗(On Diff #1392)

An amount divided by an amount is unitless.

85 ↗(On Diff #1392)

dito.

src/test/amount_tests.cpp
72 ↗(On Diff #1392)

This should test negative values as well.

This revision now requires changes to proceed.Sep 21 2017, 19:23
src/amount.h
73 ↗(On Diff #1392)

I'll change the cast style. However, unless this is explicitly cast, it will be integer division and truncate:

int main(void) {
    int64_t a = 10;
    int64_t b = 20;
    std::cout << "Output: " << a / b << std::endl;
    std::cout << "Output: " << double(a) / double(b) << std::endl;
}
> g++ test.cpp -o test && ./test
Output: 0
Output: 0.5
80 ↗(On Diff #1392)

This is modulo though, which has the units of the original thing that was left-over from the division.

schancel edited edge metadata.
  • Remove is_integral template useage
  • Use 'delete' to ensure operations against double are invalid
  • Update tests
schancel marked 8 inline comments as done.
  • Add const qualifiers to Amount operator methods
deadalnix requested changes to this revision.Sep 25 2017, 11:35
deadalnix added inline comments.
src/amount.h
75 ↗(On Diff #1408)

That shouldn't be a double ops.

82 ↗(On Diff #1408)

int64_t

This revision now requires changes to proceed.Sep 25 2017, 11:35
schancel added inline comments.
src/amount.h
82 ↗(On Diff #1408)

I don't know what you're telling me. We need both the int and the int64_t (which is the next line) for the delete trick to work.

As for the return value, as stated previously, a remainder has the units of the dividend.

src/amount.h
82 ↗(On Diff #1408)

Ha yes sorry, you are right.

schancel edited edge metadata.
  • Fix types for Amount division operator
src/amount.h
79 ↗(On Diff #1414)

This should return an int64_t or just not exist.

  • Change return type for Amount % Amount
src/amount.h
79 ↗(On Diff #1414)

Sorry but you were right, this is an amount. The formula is

A = Q*B + R

So R is the same type as A, which is an amount. However, I'm not sure this function should exist at all.

  • Remove modulus operator

Good call. We'll figure the modulo when we run into it.

This revision is now accepted and ready to land.Sep 30 2017, 21:33
This revision was automatically updated to reflect the committed changes.