Anyone know how to round to a .5 or .25 ? My dosage calculator
(https://www.jwm.guide/edible-dosage-calculator) can only round to nearest integer and for cooking purposes that isn't precise enough, but currently it is too precise without rounding.
i'm using calconic, which does not have this natively. I know I can multiply x10, round, and then / 10 but that still only gets me to within .1 instead of .25 or .5
Any formulas I can use for this?
Related
I'm trying to solve a problem where I know a percentage (set by the user) but I need to find where that percentage is on a range of numbers, so for instance if the user sets it to 50% and the value range is -10 to 10 the value would be 0.
It seems simple in my head but I'm tripping up on this one.
This should work
pos = low + (high - low) * percentage
I'm trying to scale an image down in steps so as to avoid artifacts. I have an 800 square pixel image that needs to be scaled down to 100 square pixels. I want to perform the scaling in a variable number of iterations. So lets say that I want to go from 800 to 100 in 3 iterations. How do I find the ratio to apply to the image each time to achieve the desired size?
If you want to achieve a final ratio R, in N steps, then the ratio at each step would
be the N-th root of R, or equivalently, R^(1/N). For your example, R=1/8, and N=3, so
the ratio at each step would be (1/8)^(1/3), or 1/2.
>>> math.exp(math.log(100./800) / 3)
0.5
This is basically just a math question.
Heres what I am having troubles with... I am having a difficult time coming up with how to phrase the question, so bear with me. Basically I think I need to use some advanced math to accomplish this, but I do not know what I need.
I will use some illustrations to make this clear. Spam prevention doesn't let me post pictures... Here's a simple concept image though: http://radleygh.com/images/gimp-2_2011-057-00-57-26-40.bmp
Objective: Determine if several objects lie within a cone on a 2D plane
Cone Properties:
Position (x, y)
Angle (0-359)
Spread (0-359, aka Width)
Distance (0++)
I can decide the brownish lines using a simple bit of math:
Angle_A = Angle + (Spread / 2)
Angle_B = Angle - (Spread / 2)
Angle_Target = Point_Direction(origin, object_position)
Now I thought of comparing these with the position of each object with a simple if/then statement:
If (Angle_A > Angle_Target) && (Angle_B < Angle_Target) Then Angle_Target is between A and B
This works... untill Angle_A or Angle_B pass the 0-360 threshold. 0* is between 45* and 315*... but the above if statement wouldn't work. We can then determine which direction to check based on the size of the cone...
And what if the cone effect is larger than a 180* cone?
I'm not sure of the answer. I'm pretty sure I should be using Radians... But I do not understand the concept of Radians. if someone can point me in the right direction, perhaps show me an example somewhere, that would be wonderful!
I will continue to do my own research in the mean time.
You may consider a simple transformation which sets your coordinate system such that Angle_B is zero. In other words, instead of testing
Angle_B < Angle_Target < Angle_A
you may also use
0 < Angle_Target - Angle_B < Angle_A - Angle_B
If you apply a modulo 360° to all terms you're logic should work:
0 < (Angle_Target - Angle_B) % 360 < (Angle_A - Angle_B) % 360
One radian is the angle made by tracing a circle's circumference by a length equal to that circle's radius. Hence there are exactly 2*PI radians in a circle.
So 2*PI radians = 360 degrees
So to convert degrees to radians, multiply by 2 * PI, then divide by 360. (Or of course, multiply by PI, divide by 180).
However, whether you work in radians or degrees should only be dictated by the library you are using. Even then, you could write wrappers which do the above calculations.
But to the main part of your question. Consider that:
sin (theta) = sin (360 + theta).
cos (theta) = cos (360 + theta).
etc.
So if you come across your cone that goes through 0 degrees, simply add 360 to both angles of the cone.
e.g. if your cone goes from -10 to +20, simply use 350 to 380 instead.
And of course, when you test an angle, make sure you also add 360 to that and test both the original and added angles.
e.g. testing +5 (which is in your cone), you would test 5 (which fails) then 365 (which passes).
Good luck!
I am looking for a way to convert any number to a percentage in the following way:
1.00 is 50%
numbers below 1.00 approach 0% logarithmically
numbers above 1.00 approach 100% logarithmically.
x > 0. So y needs to approach 0 as x becomes infinitely small on the positive side.
I'm sure this is simple to do, but I can't recall how to do it.
try 1 / (1 + e^(1-x))
it's the logistic function shifted by 1 unit
If you want it to approach faster, you can change e to something higher
Edit:
to have f(0) = 0 you could use 1 - 2^(-x)
When you say logarithmically, do you mean asymptotically? If so, then "y needs to approach 0 as x becomes infinitely small on the positive side" just means f(0)=0 if f is continuous. In that case x/(x+1) will work: http://www.wolframalpha.com/input/?i=x%2F%28x%2B1%29
how about y = f(t) = 1 - exp(-t/tau) ?
For t near 0, y is approximately t/tau. For t approaching infinity, y asymptotically approaches 1.
As for the f(1)=0.5 approach, this can be used to solve for tau = 1/log(2).
From what you're describing, I'm hearing the graph of x cubed -- very basic, and should be efficient in most languages.
Graph http://jedsmith.org/static/S01813305.png
This was graphed with y=(x-1)^3+1 (transforms to make (1,1) the origin). You can, of course, make the results a percentage by simply scaling by 50.
You are, ultimately, trying to have an efficient solution to give you a rough percentage behavior in a programming language and not Mathematica, right?
I noticed that translating radians to degrees and vice versa is like translating a percentage to a whole number and vice versa. For example, to get 60 percent of 345 you do the following
60 * 345/100
to convert 60 degrees to radians you do
60 * 3.14/180
There is a pattern there BUT... we use 100 to compare percentages to a number. So, why do we use 180 degrees instead of 360 degrees to compare degrees to radians?
%100 percent = a whole number
360 degrees represents a whole circle
using 180 degrees is like using 50% instead of 100%
I hope I am making some sense. Can anyone answer? Thanks
The reason you use 180 degrees instead of 360 is that there are 2*pi radians in a circle, not pi. Thus you divide both 360 and 2*pi by 2 and get pi and 180.
In Mathematica, I use the handy predefined Degree constant for conversions, which is defined as Pi/180 or 2 * Pi/360.
The reason there are 2 * Pi radians in a circle is that the size of an angle in radians is the length of the arc of a circle with radius 1 that subtends it. The circumference of a circle with radius 1 is 2 * Pi. In addition to providing a clear geometrical interpretation, using radians also makes a number of other relations much more convenient; cosine is the derivative of sine, and as a result the Maclaurin series for sines and cosines are much simpler than they would be for angles expressed in degrees.
360 degrees = 2 * Pi radians
1 degree = Pi / 180 radians
I guess your question is, why there 360 degrees in a circle (or 180 in a semicircle), and why not some other more tenable number like 100.
The answer to that is the origin of degree. If you'd like to use a round figure, check out the gradian unit of angles.
PS: SO is for programming questions only. This is not programming related.
I ask this question because my lack of paying attention in school. Programming actually is the reason I ask this question because it is now that I am actually paying attention. Every programming formula uses 180 and PI to translate back and forth instead of 360. Since I haven't came across any examples, I assumed that there was only one way. Of course if I was reading a regular math book, I would of known differently.
But I understand now. Actionscript uses 180 degrees for clock wise rotation. once 180 is reached, it uses -180 back down to 0 for a full rotation. Which makes alot more sense if you want your answer to fall in the 180 degree range. and depending on if its negative or positive determines whether or not it is traveling up on the x axis or down and y axis as well. As much as I appreciate the responses, I believe this is absolutely a suitable programming question. For programmers calculating in degrees is different from your average surveyor.
Given a real life scenario, measuring a distance is always considered a absolute value, where programming this is false. which also rationalizes why we use -180 degrees.