Skip to content

Commit d633341

Browse files
Create the Decimal type (#117)
* Add a schema definition for the `Decimal` type. * Add a protobuf implementation of the Decimal type. * Updates to Decimal type: * digits -> significand (based on steering committee feedback) * Updated package names * Changed exponent from int64 to int32 (had meant to do this sooner). * Update common-components/schemas/type/decimal.md * Rename the api directory to aip to match proto package. * Update documentation to refer to `significand` rather than `digits`. * Update common-components/schemas/type/decimal.md Co-authored-by: Yusuke Tsutsumi <yusuke@tsutsumi.io> * Update common-components/schemas/type/decimal.md Co-authored-by: Yusuke Tsutsumi <yusuke@tsutsumi.io> * Consistency fixes * Add a JSON Schema for Decimal. * Add note comparing range to double and decimal64. * Convert JSON schema to YAML. * Add trailing newline to decimal.proto. * Make JSON schema examples look like JSON. * Update Decimal JSON schema to be stricter. * Remove defaults from JSON schema; comment consistency. --------- Co-authored-by: Yusuke Tsutsumi <yusuke@tsutsumi.io>
1 parent 10a0ebb commit d633341

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
$schema: https://json-schema.org/draft/2020-12/schema
2+
$id: https://example.com/product.schema.json
3+
title: Decimal
4+
description: |
5+
Represents a decimal number in a form similar to scientific notation.
6+
7+
Examples:
8+
17 === {"significand": 17, "exponent": 0}
9+
-0.005 === {"significand": -5, "exponent": -3}
10+
33.5 million === {"significand": 335, "exponent": 5}
11+
11/8 (1.375) === {"significand": 1375, "exponent": -3}
12+
13+
Note that the range of a Decmial exceeds that of a JSON `number` (double),
14+
as well as that of a `decimal64`.
15+
type: object
16+
required:
17+
- significand
18+
- exponent
19+
additionalProperties: false
20+
properties:
21+
significand:
22+
description: The significant digits of the number.
23+
type: integer
24+
format: int64
25+
exponent:
26+
description: |
27+
Represents the position of the decimal point within the significand.
28+
29+
When the exponent is 0, the value of the Decimal is simply the value of
30+
`significand`.
31+
32+
When the exponent is greater than 0, represents the number of trailing
33+
zeroes after the significant digits.
34+
35+
When the exponent is < less than, represents how many of the
36+
significant digits (and implicit leading zeroes, as needed) come after
37+
the decmial point.
38+
type: integer
39+
format: int32
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
syntax = "proto3";
2+
3+
package aip.type;
4+
5+
option cc_enable_arenas = true;
6+
option go_package = "aip.golang.org/genproto/common-components/type/decimal;decimal";
7+
option java_multiple_files = true;
8+
option java_outer_classname = "DecimalProto";
9+
option java_package = "dev.aip.type";
10+
option objc_class_prefix = "AIP";
11+
12+
// Represents a decimal number in a form similar to scientific notation.
13+
//
14+
// Examples:
15+
// 17 === {significand: 17 exponent: 0} (or just {significand: 17})
16+
// -0.005 === {significand: -5 exponent: -3}
17+
// 33.5 million === {significand: 335 exponent: 5}
18+
// 11/8 (1.375) === {significand: 1375 exponent: -3}
19+
//
20+
// Note that the range of a Decmial exceeds that of a JSON `number` (double), as
21+
// well as that of a `decimal64`.
22+
message Decimal {
23+
// The significant digits of the number.
24+
int64 significand = 1;
25+
26+
// Represents the position of the decimal point within the significand.
27+
//
28+
// When the exponent is 0, the value of the Decimal is simply the value of
29+
// `significand`.
30+
//
31+
// When the exponent is greater than 0, represents the number of trailing
32+
// zeroes after the significant digits.
33+
//
34+
// When the exponent is less than 0, represents how many of the significant
35+
// digits (and implicit leading zeroes, as needed) come after the decmial
36+
// point.
37+
int32 exponent = 2;
38+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Decimal
2+
3+
The `Decimal` type represents a decimal numeric value. It uses a non-floating
4+
point representation in order to avoid precision errors.
5+
6+
## Schema
7+
8+
A `Decimal` represents a decimal number in a form similar to scientific
9+
notation.
10+
11+
It has two fields:
12+
13+
- The `significand` field is a 64-bit integer representing the significant
14+
digits of the number.
15+
- The `exponent` field is a 32-bit integer represesnting the position of the
16+
deicmal point within the value of the `significand` field; it is the power of
17+
ten to which `significand` should be raised.
18+
19+
When the exponent is `0`, the value of the `Decimal` is simply the value of
20+
`significand`.
21+
22+
When the exponent is greater than 0, represents the number of trailing zeroes
23+
after the significant digits.
24+
25+
When the exponent is less than 0, represents how many of the significant
26+
digits (and implicit leading zeroes, as needed) come after the decimal point.
27+
28+
The general formula for converting a `Decimal` to its numeric value is
29+
`significant * 10^exponent`, where `*` represents multplication and `^`
30+
represents exponentiation.
31+
32+
Note: The range of a Decmial exceeds that of a JSON `number` (double), as well
33+
as that of a `decimal64`.
34+
35+
## Examples
36+
37+
- 17 === `{significand: 17, exponent: 0}`
38+
- -0.005 === `{significand: -5, exponent: -3}`
39+
- 33.5 million === `{significand: 335, exponent: 5}`
40+
- 11/8 === 1.375 === `{significand: 1375, exponent: -3}`

0 commit comments

Comments
 (0)