How to get the average of two floating point numbers - operator-overloading

I want to get the average of a 2 floating point numbers. My function for the integer variant
let int_average x y = (x + y) / 2
works fine but when I try to write it for floats
let float_average x y = (x +. y) / 2.
it fails with the error
This expression has type float but an expression was expected of type
int

You forgot to "floatify" the division operator. / should be /., just as +. is the float variant of +:
let float_average x y = (x +. y) /. 2.

Related

How to resolve an equation in dart (from a string)

I'm trying to resolve equations in dart. Here an example of an equation: y = x * 16. The y variable is a known value and x the value I want to compute. For example, if y = 32, I want to resolve the equation 32 = x * 16 (so 2).
I can't transform the equations manually because they come from an external source. Some examples : y = x * 16, y = (x + 35) / 1500, eventually y = (x + 14) - (x - 5). The left operand is always y (a known value). I can eventually simplify the right operand manually.
I tried to use the math_expressions and equations packages, but without success.

Scala: recursion and stack overflow error

I'm trying to do the following exercise:
Define a recursive function inteDef that approximates the definite integral of a function f on the interval [a,b] using the composite trapezoidal rule. The function will take eps as a parameter, which marks the maximum length of the subintervals into which the interval [a,b] will be divided. Thus, if the length of [a,b] is less than or equal to eps, the function will return the
area of the corresponding trapezoid. Otherwise, it will divide the interval [a,b] into two subintervals of the same length (half of the original interval) and it will recursively approximate the integral of both intervals and return their sum.
My solution attempt is:
def inteDef(f: Double => Double, x: Double, y: Double, eps: Double): Double = {
if (abs(y - x) <= eps)
(y - x) * (f(x) + f(y)) / 2.0
else
inteDef(f, x, (y-x)/2.0, eps) + inteDef(f, (y-x)/2.0, y, eps)
}
It works when eps is greater than or equal to abs(y - x), but gives a stack overflow error otherwise.
(y - x) / 2.0 is not in the middle between x and y.
(x + y) / 2.0 is.

Swift Double.remainder(dividingBy:) returning negative value

let num = 32.0
Double(num).remainder(dividingBy: 12.0)
I'm getting -4?..instead of 8.0...it's subtracting 12.0 from 8.0
how do i fix this?
Please, read the documentation carefully:
For two finite values x and y, the remainder r of dividing x by y satisfies x == y * q + r, where q is the integer nearest to x / y. If x / y is exactly halfway between two integers, q is chosen to be even. Note that q is not x / y computed in floating-point arithmetic, and that q may not be representable in any available integer type.
(emphasis mine)
You want to use truncatingRemainder(dividingBy:) instead:
let num = 32.0
let value = Double(num)
.truncatingRemainder(dividingBy: 12)
print(value) // 8
remainder(dividingBy:)is not the modulus function.
In real division 32.0/12.0 = 2.666666.... The remainder(dividingBy:) function defines the nearest integer to that result as q: in this case 3. So we write:
32.O = q * 12 + r
With q being an integer, and r a Double.
32.0 = 3 * 12.0 + r ⇒ r = - 4.0
The remainder r, as defined by this function, is -4.0.

initial point in CORDIC algorithm

I am trying to reduce number of iterations required to calculate multiplication using the CORDIC algorithm because I am using this algorithm in a continuous function to calculate square function. Here is the algorithm assuming -1<x<1'
function z=square(x)
y=x;
z=0;
for i=1:15
if (x > 0)
x = x - 2^(-i);
z = z + y*2^(-i);
else
x = x + 2^(-i);
z = z - y*2^(-i);
end
end
return
end
I already know the close value to multiplication result (from the previous result (call it pr)) and value of x (the value of x is continuous) . Does it help in anyway to decrease number of iterations?
If you are multiplying twice by the same constant, say a.x and a.x', then you can multiply and add with the delta a.(x' - x), which has less significant digits.
In case both factors vary, you can still use
x'.y' = (x'- x).(y' - y) + x.(y' - y) + (x' - x).y + x.y
where maybe the first term is neglectible.
For a square,
x'² = (x'- x)² + 2.x.(x' - x) + x²

little mathematical thing : squares and roundings

in scala, given the integers d & x, I would have a boolean expression which should be true if and only if y = (x^2 - 1) / d^2 is a square.
I tried this:
(Math.sqrt((x * x - 1) / (d * d)).toInt * Math.sqrt((x * x - 1) / (d * d)).toInt == ((x * x - 1) / (d * d)))
but the 3-tuple (x = 2, d = <all values tested>, y = 0.0) seems to be always an answer of my problem, which is obviously wrong.
I think my error comes from the rounding made: if x=2, d=4 (for example) then x * x - 1 == 3 and d * d == 16 so the division leads to 0.
do you know what is the good expression?
if n is a round square, then Math.sqrt(n).toInt == Math.sqrt(n). In your case:
(Math.sqrt((x * x - 1) / (d * d)).toInt == Math.sqrt((x * x - 1) / (d * d)))
But before doing that, you need to make sure that x and d are doubles.
Try in REPL:
scala> val x = 1
scala> val d = 3
scala> x/d
A Int divided by an Int will result the rounded Int, so you are applying sqrt to zero.
Also due to float point arithmetic, you may want to compare like this instead:
(Math.sqrt((x * x - 1) / (d * d)).toInt - Math.sqrt((x * x - 1) / (d * d))) <= ZERO
where ZERO is replaced by a really small number like 0.00001
Because this is integer division, you are checking whether ((x*x-1)/(d*d)).toInt is a perfect square. You can convert everything to doubles first, but if you want to stay in the realm of integers, check that the division should result in an integer:
( x*x-1 % d*d == 0 ) && Math.sqrt(y).toInt == Math.sqrt(y)