How to multiply two int variables in Ruby - ruby

I am making a program to enter the radius and output the area and the perimeter:
# Calculates the area and the perimeter of a circle given the radius
puts "Enter a radius"
radius = gets
area = radius * 3.14 * radius
perimeter = 2 * 3.14 * radius
puts area
puts perimeter
When I tried to execute the code, it returned:
area.rb:4:in `*': no implicit conversion of String into Integer (TypeError)
The compiler says that the error is on the fourth line, but far I don't see any problems.

The problem is that radius is a string, because gets always returns a string. Maybe it contains an integer, but it's a string representation of an integer, like '10.3'.
radius = gets.to_i
or if you need decimal values
radius = gets.to_f
It's a bit more complex than this because to_i and to_f give you 0 and 0.0 respectively if you called them on 'foo'. In that case you could use the Integer and Float methods, and they will give you an ArgumentError exception if they are not able to convert to an integer or float value.

Related

Calculate the value of PI using `pi = x * sin(180 / x)`

I have a problem using sin. I did:
include Math
puts sin(5) # => -0.9589242746631385
But when I type sin(5) into my calculator, it outputs 0.087155742. Likewise, I tried to calculate the value of PI using the equation pi = x * sin(180 / x) and got a problem although I got the value of PI with it on my calculator.
include Math
puts "Enter the value of x"
x = gets.to_f
num = 180 / x
pie = x * sin(num)
puts pie
Thanks if anyone can help.
Your equation is for a calculation of the sin of an angle in degrees.
Most, maybe all, computer language sin() functions expect the angle to be in radians.
Unfortunately, you need to have a value for pi to convert from one to another, so unless you can find a sin-in-degrees implementation you’re somewhat stuck.
This is because your formula comes from the 2×π/360 or π/180 conversion between radians and degrees.

How to convert float to percentage in Ruby

I need to turn a floating point number (0.02) into a percentage(2%). What is they syntax in ruby?
To convert a float to a percentage, multiply the float by 100. If you need to output it with the % sign, use string interpolation inside a string.
Example:
percentage_value = 0.02 * 100
puts "#{percentage_value}%"
2.0%
{edited}

Matlab range - gamma correction

Could you tell me how to take a range of an image? I want to do a gamma correction of "cameraman.tif" for pixels in range 0:120 gamma = 1.8, for pixels in range 120:255 gamma = 0.5
But all pixels go to first if statement so I am unable to apply second gamma.
a = imread('cameraman.tif');
gamma1 = 2;
gamma2 = 0.5;
N =size(a);
out = zeros(N);
for i=1:N
for j=1:N
temp=a(i,j);
if temp>0&temp<120
out(i,j)=temp.^2;
end
if temp>120&temp<=255
out(kx,ky)=temp.^0.5;
end
end
end
imshow(out)
Your second if statement uses access variables kx and ky.... I'm assuming you wanted to use i and j:
out(i,j)=temp.^0.5;
You also must make sure that the intensity is double precision for the square root to work. Therefore make sure that the intensity read in per location is cast to double, then convert back to uint8 when you're done. Actually, do the conversion after you sweep through the whole image.
for i=1:N
for j=1:N
temp=double(a(i,j)); % Change
if temp>0&temp<120
out(i,j)=temp.^2;
end
if temp>120&temp<=255
out(i,j)=temp.^0.5; % Change
end
end
end
out = uint8(out); % Change
kx and ky were set somewhere else in your code and are never changing, so this means that if and when the second if statement does happen, the setting of the gamma only happens at one spot only defined at kx and ky. My advice to you would be to write an actual function so that you aren't cross-contaminating variables in different workspaces. Encompassing this in a function would have given you an error immediately telling you that kx and ky are not defined.
BTW, I would rather you do this more efficiently without loops. You can very easily perform the same operations vectorized. However, this requires that you convert the image to double as the default type is uint8 for the Cameraman image. Therefore, use double to convert an image to double, do the gamma correction, then convert back using uint8:
a = double(imread('cameraman.tif'));
out = zeros(size(a));
out(a > 0 & a < 120) = a(a > 0 & a < 120).^2;
out(a >= 120 & a <= 255) = a((a >= 120 & a <= 255).^0.5;
out = uint8(out);
The first and second line of code are of course familiar. The third line of code finds a logical mask where we search for intensities between 0 and 120 exclusive. Once we find those values, we use the same logical mask to index into the original image and only access those values, square each value and set them in the same spatial locations at the output. The same can be said for the last line of code where you're searching between 120 and 255 but you are taking the square root instead. We finally convert to uint8 for display.

Random numbers in the shape of a bell curve [duplicate]

This question already has answers here:
Generate random numbers following a normal distribution in C/C++
(18 answers)
Normal Distributed Random Number in VB.NET
(2 answers)
Closed 7 years ago.
I need to manipulate random to produce a statistical average with the shape of a bell curve, here is my attempt:
Function slopedrand(max As Single, bias As Single) As Integer
Dim count As Single = 0
While count < bias
max = rand.NextDouble() * max
count += rand.NextDouble()
End While
Return rand.NextDouble() * max
End Function
Function bulgedrand(min As Single, max As Single, bulge As Single, bias As Single)
Dim negative = bulge - min
Dim positive = max - bulge
Dim nr = min + slopedrand(negative, bias)
Dim pr = max - slopedrand(positive, bias)
Return rand.NextDouble() * (pr - nr) + nr
End Function
however, given that I suck at math, all it produces is stuff like this: http://i.imgur.com/JDAW6kM.png
which is more like a bulge...
Since it feels like my skull is boiling, maybe somebody here has figured out how to accomplish what I need and will spare me from further attempts of thinking?
(the example is given in vb.net since it was quick to prototype on but any language is welcome)
The "established" way of doing this is to draw a uniformly distributed random number in the range [0, 1), then apply the quantile function of the Bell curve distribution. This then gives you a random number that has the same distribution as the Bell curve.
A bell curve can be approximated by a normal distribution, so I guess you can use random values inside the formula for a normal distribution and get the effect you need, such as in this answer:
drawing random number from a standard normal distribution using the standard rand() functions
Maybe it can help you?

Chordal Catmull-Rom Splines

I've been working on getting Catmull-Rom splines working for a side project and am having difficulty getting it to do what I need. I tried the following two implementations and both didn't work for me, and I was unable to track down any errors in my code relative to theirs (which I have to assume has been tested). I'll call theirs the "ABC" solution:
Catmull-rom curve with no cusps and no self-intersections
https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
I then implemented the following solution (that I call the "Matrix" solution) and it did work using the edited version 3 posts down: https://www.opengl.org/discussion_boards/showthread.php/159518-catmull-rom-spline
However, this Matrix solution just implements Catmull-Rom with a 0.5 'a' value built into the matrix. I'd like to get Chordal working, and thus I need 'a' == 1.
Given that my solution for the ABC version was causing problems, I've attempted to use the matrix here (http://algorithmist.net/docs/catmullrom.pdf) to pass in my own 'a'. Here's the original 0.5 code followed by my modified code that's passing in a user specified 'a'.
Original Code:
float u2 = u * u;
float u3 = u2 * u;
return ((2 * x1) +
(-x0 + x2) * u +
(2*x0 - 5*x1 + 4*x2 - x3) * u2 +
(-x0 + 3*x1 - 3*x2 + x3) * u3) * 0.5f;
Modified Code:
float u2 = u * u;
float u3 = u2 * u;
static float a = 0.5f;
return ((1.0f * x1) +
((-a*x0) + (a*x2)) * u +
((2.0f*a)*x0 + (a-3.0f)*x1 + (3.0f-(2.0f*a))*x2 + (-a*x3)) * u2 +
((-a*x0) + (2.0f-a)*x1 + (a-2.0f)*x2 + (a*3.0f)) * u3) * 0.5f;
This of course doesn't work. However, I'm not seeing why. At the bottom of page 4 in the pdf it shows the matrix with 'a' in it. I've substituted that in the above modified code and triple checked it, yet the spline is screwed up. It should have given me the same answer. What's doubly confusing is that his results on page 5 take that resulting matrix and multiply it by 0.5 which drops all the /2's off the matrix entries. The final matrix uses THESE values, but the original matrix on page 4 is not 0.5 * matrix, it's just "matrix". Why was this 0.5 arbitrarily added and why does everything break without it?
Anyway, does anyone know what I might be doing wrong with my equation? Can I use this matrix form to pass in my own 'a' from 0-1 and create uniform, centripetal and chordal splines or will I have to use the ABC form?
Thanks in advance!
I think the matrix with 'a' in page 4 of the pdf file is still for uniform Catmull-Rom (CR) spline. The parameter 'a' is the tension parameter. In the Wiki page (https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline), it also use a 'alpha' for controlling the knot sequences assigned to each point. Don't confuse the tension parameter 'a' with this 'alpha'.
A "standard" uniform CR spline will have alpha=0.0 (which will result in a=0.5). You will need to use alpha=1.0 for chordal CR spline and alpha=0.5 for centripetal CR spline. Their corresponding matrix form will both involve the knot sequences of the points. So, using a=1.0 in the matrix form for uniform CR spline will not result in a chordal CR spline but a uniform CR spline with stronger tangents at the data points, which typically will cause undesired spline shape.

Resources