Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

25
LINES

< > BotCompany Repo | #1032246 // RatioAccumulator - can add to numerator and denominator

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3091L/17K).

1  
sclass RatioAccumulator {
2  
  double numerator, denominator;
3  
  
4  
  void add(double numerator, double denominator) {
5  
    this.numerator += numerator;
6  
    this.denominator += denominator;
7  
  }
8  
  
9  
  // by default we return the initial state (undefined ratio) as 0
10  
  public double get() {
11  
    ret doubleRatio(numerator, denominator);
12  
  }
13  
  
14  
  bool valid() { ret denominator != 0; }
15  
  
16  
  // ...but you can also get it as null
17  
  public Double getOrNull() {
18  
    ret valid() ? get() : null;
19  
  }
20  
  
21  
  // ...or NaN (my least preferred method)
22  
  public Double getOrNaN() {
23  
    ret valid() ? get() : Double.NaN;
24  
  }
25  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032246
Snippet name: RatioAccumulator - can add to numerator and denominator
Eternal ID of this version: #1032246/2
Text MD5: 7583b2ad0c219e6e1e2a28002b446664
Transpilation MD5: 422acbf699271e0e039f74c9338b5787
Author: stefan
Category: javax / maths
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-22 21:03:30
Source code size: 625 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 164 / 293
Version history: 1 change(s)
Referenced in: [show references]