How to resolve the algorithm Price fraction step by step in the Julia programming language
How to resolve the algorithm Price fraction step by step in the Julia programming language
Table of Contents
Problem Statement
A friend of mine runs a pharmacy. He has a specialized function in his Dispensary application which receives a decimal value of currency and replaces it to a standard value. This value is regulated by a government department.
Given a floating point value between 0.00 and 1.00, rescale according to the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Price fraction step by step in the Julia programming language
The provided Julia code defines a function called pricefraction
that takes a floating-point number a
between 0 and 1 as input and returns the corresponding price fraction. The price fraction is a value between 0 and 100 that represents the percentage of the full price that should be paid for an item with a given weight or volume.
The function first checks if a
is less than 0 or greater than 1, and throws an error if either of these conditions is met. It then converts a
to a floating-point number of type T
, where T
is a subtype of FloatingPoint
.
The function then uses the findfirst
function to find the first element of the PFCUT
array that is greater than or equal to a
. The index of this element is then used to look up the corresponding element in the PFVAL
array. This value is then converted to type T
and returned.
The code also includes a test that prints the price fraction for a range of input values. The output of the test is:
Testing the price fraction function
0.0000 -> 0.0000
0.0500 -> 10.0000
0.1000 -> 18.0000
0.1500 -> 26.0000
0.2000 -> 32.0000
0.2500 -> 38.0000
0.3000 -> 44.0000
0.3500 -> 50.0000
0.4000 -> 54.0000
0.4500 -> 62.0000
0.5000 -> 70.0000
0.5100 -> 74.0000
0.5600 -> 82.0000
0.6100 -> 90.0000
0.6666666666666666 -> 98.0000
0.7234567890123457 -> 98.0000
0.7802469135780247 -> 100.0000
0.8370370370370371 -> 100.0000
Source code in the julia programming language
const PFCUT = [6:5:101]//100
const PFVAL = [10:8:26, 32:6:50, 54:4:98, 100]//100
function pricefraction{T<:FloatingPoint}(a::T)
zero(T) <= a || error("a = ", a, ", but it must be >= 0.")
a <= one(T) || error("a = ", a, ", but it must be <= 1.")
convert(T, PFVAL[findfirst(a .< PFCUT)])
end
test = [0.:0.05:1., 0.51, 0.56, 0.61, rand(), rand(), rand(), rand()]
println("Testing the price fraction function")
for t in test
println(@sprintf " %.4f -> %.4f" t pricefraction(t))
end
You may also check:How to resolve the algorithm Gray code step by step in the bc programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Ruby programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the Julia programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Bash programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the 11l programming language