How to resolve the algorithm Pathological floating point problems step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Pathological floating point problems step by step in the jq programming language

Table of Contents

Problem Statement

Most programmers are familiar with the inexactness of floating point calculations in a binary processor. The classic example being: In many situations the amount of error in such calculations is very small and can be overlooked or eliminated with rounding. There are pathological problems however, where seemingly simple, straight-forward calculations are extremely sensitive to even tiny amounts of imprecision. This task's purpose is to show how your language deals with such classes of problems.

A sequence that seems to converge to a wrong limit. Consider the sequence:

As   n   grows larger, the series should converge to   6   but small amounts of error will cause it to approach   100.

Display the values of the sequence where   n =   3, 4, 5, 6, 7, 8, 20, 30, 50 & 100   to at least 16 decimal places.

The Chaotic Bank Society   is offering a new investment account to their customers. You first deposit   $e - 1   where   e   is   2.7182818...   the base of natural logarithms. After each year, your account balance will be multiplied by the number of years that have passed, and $1 in service charges will be removed. So ...

What will your balance be after   25   years?

Siegfried Rump's example.   Consider the following function, designed by Siegfried Rump in 1988.

Demonstrate how to solve at least one of the first two problems, or both, and the third if you're feeling particularly jaunty.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pathological floating point problems step by step in the jq programming language

Source code in the jq programming language

# Input: the value at which to compute v
def v:
  # Input: cache
  # Output: updated cache
  def v_(n):
    (n|tostring) as $s
    | . as $cache
    | if ($cache | has($s)) then .
      else if n == 1 then $cache["1"] = 2
           elif n == 2 then $cache["2"] = -4
           else ($cache | v_(n-1) | v_(n - 2)) as $new
           | $new[(n-1)|tostring] as $x
 	   | $new[(n-2)|tostring] as $y
           | $new + {($s):  ((111 - (1130 / $x) + (3000 / ($x * $y)))) }
	   end
       end;
    . as $m | {} | v_($m) | .[($m|tostring)] ;

(3,4,5,6,7,8,20,30,50,100) | v

# Given the balance in the prior year, compute the new balance in year n.
# Input: { e: m, c: n } representing m*e + n
def new_balance(n):
  if n == 0 then {e: 1, c: -1}
  else {e: (.e * n), c: (.c * n - 1) }
  end;

def balance(n):
  def e: 1|exp;
  reduce range(0;n) as $i ({}; new_balance($i) )
  | (.e * e) + .c;

balance(25)

  

You may also check:How to resolve the algorithm Rate counter step by step in the Ada programming language
You may also check:How to resolve the algorithm 24 game/Solve step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Box the compass step by step in the AutoIt programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the Delphi programming language
You may also check:How to resolve the algorithm String matching step by step in the Go programming language