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).

sclass RatioAccumulator {
  double numerator, denominator;
  
  void add(double numerator, double denominator) {
    this.numerator += numerator;
    this.denominator += denominator;
  }
  
  // by default we return the initial state (undefined ratio) as 0
  public double get() {
    ret doubleRatio(numerator, denominator);
  }
  
  bool valid() { ret denominator != 0; }
  
  // ...but you can also get it as null
  public Double getOrNull() {
    ret valid() ? get() : null;
  }
  
  // ...or NaN (my least preferred method)
  public Double getOrNaN() {
    ret valid() ? get() : Double.NaN;
  }
}

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: 163 / 292
Version history: 1 change(s)
Referenced in: [show references]