i have the below actionscript and looking for the equivalent ruby on rails code to do the same job.
where dbwall and db_wall = 50
var tb= Math.pow((1/10),(dbwall)/(10));
Currently i have used:
#tb = ((1/10) ** (db_wall)/10)
and get 0.0, which is not what i need:
however in actionscript the actual answer to this is 0.00001
Is it just a case of the correct formula but not showing decimal places? or is there more to it than that?
Integer division in Ruby returns an integer (like C) so 1/10 equals 0. To get Float division, explicitly make one of the operands a Float e.g. 1.0/10
You don't need to include a few of the brackets too.
#tb = ((1/10) ** db_wall/10)
Would simply work. I wouldn't make this variable accessible to the view by using # if you carry on, just take that off and use it in future calculations.
What Max has answered would be the best answer to this simple problem.
Related
I'm trying to make a "complex calculation" in a freemarker template. The calculation calls for a modulus call. At first I kept simplifying my code, and finally just hardcoding values to try and figure out the cause, but it seems that freemarker's mod function ALWAYS returns int values?? but they're not even rounded, they're truncated. I need them rounded (.5 up would work). My most simplified code that does NOT work is this:
<#assign p_year_m1 = (2503.638 % 7.00)?float>
the longer version is:
<#assign p_year_m1 = ((bdy_m1 + (bdy_m1/4.0)-(bdy_m1/100.0)+(bdy_m1/400.0)))%7>
So the correct answer should be 4.638, rounds up to 5, but I'm getting 4.00 even if I wrap ?string[0.00] around it.
What am I missing? Surely there has to be a way to make this work! If not, I guess I'd need help constructing a work around to get similar results.
Thanks.
PS - the var, p_year_m1 is not intialized before this, so I believe if I try to store a float in there it should hold a float.
This is the calculation I'm trying to replicate
I can confirm that % gives the remainder of integer division. It casts (truncates) both arguments to integers, and then calculates the remainder. It's weird for sure, but can't be changed because of backward compatibility. The main application is calculating "zebra tables" and such, where it's might as well desirable, I don't know... it had just get in like that in the early times of the project, and then stuck.
I am having real difficulty with this and every answer I have seen doesnt seem to work. I have been able to pass a value such as 1.44 as 1.00 but the two decimal values are being lost. I have a number of values passed from a from which i then want to submit to an api via a call. The code is below:
IncomeWagesWeekly = params[:WagesWeekly].to_i
How do i ensure that when this is passed the two decimal places are present. Thanks for any help.
You don't have in Ruby language such thing as fixed digits after decimal point.
1 is the same as 1.00 as it was rightfully mentioned before in comment (almost the same).
If you don't check it's type like that:
1.is_a? Integer # => true
1.is_a? Float # => false
it is all the same.
Just use 1.44.to_i.
If by some reason you want to have an instance of Float instead use to_f method. To crop number explicitly you should use round, or ceil, or floor method:
1.44.round.to_f # => 1.0
1.55.round.to_f # => 2.0
1.44.ceil.to_f # => 2.0
1.55.ceil.to_f # => 2.0
1.44.floor.to_f # => 1.0
1.55.floor.to_f # => 1.0
Maybe don't try to make in integer and use to_f instead. Also consider any sprintf( "%0.02f", your_number), but it returns string.
First, it seems that you want to do fixed-point arithmetic here. So, using a floating point number is not the right choice, since floating point calculations can produce mathematically incorrect results.
A solution for this would be either to stick with Integers (as has been suggested already), or to use the data type BigDecimal, which is defined in the Ruby standard library, and in particular its methods fix, frac and to_digits.
Now to your database: You didn't say what database you are using, and how you pass the values to it, but in general, it is a bad idea to store a non-integer value into a database-field which is supposed to accept integers. As you observed, the fractional part was dropped. Correct behaviour.
You could redefine your database schema, or you can convert your decimal value by yourself into something which matches the field definition in the database. Which way to go, depends on what you want to actually do with this value afterwards. For instance, if you just want to display it, but not perform any calculations, you could use a string. Or, if you know that the number of fractional digits don't exceed a certain limit, you could define a suitable numeric format for the database column - etc.
I got a little problem that I really would like to understand. I am using assert_equal to compare two BigDecimal numbers that are supposed to be identical. They actually are except a very little tiny fraction, see below:
-#<BigDecimal:7f4b40e8de78,'0.4021666666 6666666666 666666667E2',36(45)>
+#<BigDecimal:7f4b40e85db8,'0.4021666666 6666666666 6666668E2',36(63)>
I use assert_in_delta in order to not fail the test cases. So I got a reasonable workaround. I do wonder though whether it would be possible to have it equal:
assert_equal (241.30.to_d/6), model.division_function
The model's division_function does exactly the same. It divides a BigDecimal of value 241.3 by the length of an array, which is 6.
There seems to be a very tiny difference in the precision.
I would like to know where that might come from?
Is there a way I can control precision more accurately?
EDIT
I am using Mongoid. It is worth to note that Mongoid offers BigDecimal as a field type, but it is stored as a string. However, I don't think this is the problem. I believe it is a Ruby thing.
EDIT
I got a little further with an example which hints that it is a Ruby issue and not directly related to Rails. Please see below:
irb(main):041:0* amount1 = BigDecimal("241.3")
=> #<BigDecimal:7f49bcb03558,'0.2413E3',18(18)>
irb(main):042:0> amount2 = BigDecimal("1800")
=> #<BigDecimal:7f49bcaf3400,'0.18E4',9(18)>
irb(main):043:0> rate = amount1 / amount2
=> #<BigDecimal:7f49bcae8398,'0.1340555555 5555555555 5555556E0',27(45)>
irb(main):044:0> rate * amount2 #should return amount1 = 241.3, but it does not :-(
=> #<BigDecimal:7f49bcad6a30,'0.2413000000 0000000000 00000008E3',36(45)>
irb(main):045:0>
I reported the bug to the Ruby core team. However, this is not a bug as you can see in the rejection response.
BigDecimal, though offers arbitrary precision, cannot represent numbers like 1/3 precisely. Thus during some arithmetic you can encounter imprecisions.
You can use Rational in Ruby for exact numbers. Exercise caution when doing arithmetics if you wish to keep the result exact.
When I run this script:
pi=3.14
NozzleAreaM=.005**2*pi.to_f
It returns 7.85e-05 instead of 0.000078 like it does on a normal calculator. I've looked everywhere and I can't find a fix to this problem.
You just need to tell ruby in what format you want to display the float:
pi = 3.14
nozzle_area = 0.005**2 * pi
printf "%.8f \n", nozzle_area
--output:--
0.00007850
See prinf/sprinf here:
http://www.ruby-doc.org/core-2.2.0/Kernel.html#method-i-sprintf
Note that displaying a float in a certain format does not affect how ruby stores the float internally.
Also, writing code like this is fine:
pi=3.14
NozzleAreaM=.005**2*pi.to_f
...as long as you never need to show your code to anyone else. If you want to ask questions about your code, then you need to write legible code. That means you should NOT cram every thing together with no spaces.
In addition, variables that start with a capital letter are constants in ruby. If you do not know what that means yet, just live by this rule: do not capitalize any of your variable names.
Also, your pi variable is already a float (because it has a decimal point), so calling to_f() is unnecessary.
Also, ruby provides pi as a constant in the Math module(the Math module is required automatically):
nozzle_area = 0.005**2 * Math::PI
printf "%.8f \n", nozzle_area
--output:--
0.00007854
The following piece of code works perfectly in script/console but returns the following error when i compile the same in a ruby script.:
:in `round': wrong number of arguments (1 for 0) (ArgumentError)
tf={"ph"=>{0=>1.33333333333333, 1=>1.5}, "fee"=>{0=>1.66666666666667}, "test"=>{0=>1.16666666666667, 1=>1.25}, "what"=>{0=>2.0, 1=>2.0}, "for"=>{0=>1.5}, "is"=>{0=>1.83333333333333, 1=>1.75}}
tf.each{|k,v| v.each{|k1,v1| tf[k][k1]=(v1.round(5))}}
Any Ideas ? Cheers !
Float#round seems to work differently in Ruby 1.8 and Ruby 1.9: in 1.8 it complains about the given argument, in 1.9 returns back float properly rounded to the given number of decimals.
But, as the article linked in the other answer wisely says:
you should consider the reason you’re
performing the rounding (or
equivalent) operation. If it’s for
presentation reasons only a better way
might be to use a format string
instead, and leave the original data
intact.
From what it looks like, you are not supposed to pass an argument to the round method. You have passed 5 to it.
If you are trying to round it to 5 decimal places, there is no builtin method for that (that I'm aware of). This is a page that explains how to do so: http://solutions.hans-eric.com/rounding-off-floating-point-numbers-in-ruby