Ruby Float Issue [duplicate] - ruby-on-rails

This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 8 years ago.
Should be Simple.
I'm using Rails and doing something along the lines of:
<% n = 15 / 2 %>
<%= n %>
However, whenever I output variable n, it doesn't give me a decimal.
Based on other calculations it looks like it's constantly rounding to floor, But I haven't added any sort of round method anywhere.
Could you explain why? I want it to actually round to ceil.

You can use Numeric#ceil
n = 15 / 2.0
n = n.ceil
Now, you need to either use .to_f or specify the numerator or denominator as a float ( 2.0 or 15.0) in order for it to be considered a floating point expression (as against an integer expression)

You asked for explanation as well - the explanation is that because there are no decimal points, ruby is treating your numbers as integers. When integer division is done in ruby, anything after the decimal point is truncated. In order to not lose that info, you need to tell ruby to do floating point arithmetic, which you can do in either of the ways Karthikr explained.

The issue is that 15 and 2 are not Floats but are Integers. You are doing Integer math.
You can coerce either to a float by using .to_f or adding that .0 and you will not have the same issue. If you want it to round up, then you can of course use .ceil
So, it isn't 'rounding to floor' it is a different concept.

Related

How is the following number stored in lua?

I have this number :
b = 1.324567890123456789
and the question was ask how was it stored in lua? Now when I type print(b), it shows that the end digits are
...1235
Now the question gave me the options of
...12345
or
...12346
or the option of none on the list -- and i was just wondering if anyone could help me solve this?
By default, Lua stores real numbers as double precision floating point values.
print calls tostring, which converts doubles to strings using "%.14g". [1]
Use string.format("%.17g",b) if you want more decimals.
[1] https://www.lua.org/source/5.3/luaconf.h.html#LUA_NUMBER_FMT

Why is output of {42.05 + 0.05 } like this on Dart Lang? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When i try this on DartPad output is like this. Anyone can explain ?
This is expected behavior. Double numbers cannot represent all decimal fractions precisely, and neither 0.05 nor 42.05 are the exact values that the double values represent.
The exact values are:
42.0499999999999971578290569595992565155029296875
0.05000000000000000277555756156289135105907917022705078125
If you add these two exact values, the result can yet again not be represented exactly as a double. The two closest representable doubles are:
42.099999999999994315658113919198513031005859375
42.10000000000000142108547152020037174224853515625
Of these, the former is closer to the correct result of the addition, so that is the double value chosen to represent that result.
This issue is not specific to Dart. All language using IEEE-754 64-bit floating point numbers will get the same result, and that is probably all languages with a 64-bit floating point type (C, C++, C#, Java, JavaScript, etc).

Converting Double to NSNumber in Swift Loses Accuracy [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
For some reason, certain Doubles in my Swift app are giving me trouble when converting to NSNumber, while some are not. My app needs to convert doubles with 2 decimal places (prices) to NSNumbers so they can be stored and retrieved using Core Data. For example, a few particular prices such as 79.99 would evaluate to 99.98999999999999 unless specifically formatted using NSNumber's doubleValue method.
Here selectedWarranty.price = 79.99 as shown in debugger
// item.price: NSNumber?
// selectedWarranty.price: Double?
item.price = NSNumber(double: selectedWarranty.price!)
I programmed some print statements to show how the conversion works out
Original double: 79.99
Converted to NSNumber: 79.98999999999999
.doubleValue Representation: 79.99
Can somebody explain if there is a reason why the initializer cannot surely keep 2 decimal places for every number? I would really like to store the prices in Core Data like they should be. Formatting every time it is displayed doesn't sound very convenient.
UPDATE:
Converted Core Data object to type NSDecimalNumber through data model, 79.99 and 99.99 no longer a problem, but now more manageable issue with different numbers...
Original double: 39.99
Converted to NSDecimalNumber: 39.99000000000001024
Firstly, you're confusing some terms. 79.98999999999999 is higher precision than 79.99 (it has a longer decimal expansion), but lower accuracy (it deviates from the true value).
Secondly, NSNumber does not store neither 79.99 nor 79.98999999999999. It stores the magnitude of the value according to the IEEE 754 standard. What you're seeing is likely the consequence of the printing logic that's applied to convert that magnitude into a human readable number. In any case, you should not be relying on Float or Double to store values with a fixed precision. By their very nature, they sacrifice precision in order to gain a longer range of representable values.
You would be much better off representing prices as an Int of cents, or as an NSDecimalNumber.
Please refer to Why not use Double or Float to represent currency?
That's how double works everywhere. If you need only 2 decimal places consider using integer/long instead adding point after second digit, when need to display the value.

float unusual behaviour Objective c [duplicate]

This question already has answers here:
Trouble with floats in Objective-C
(3 answers)
Closed 7 years ago.
I am getting an unusal issue with float in Objective C. I enter 100.1 and i get 100.100002 shouldn't it be something like 100.100000 .
Following is the code
float temp=100.1;
NSLog(#"%f",temp);
100.100000
Can someone guide me what am i doing wrong or how to fix it ? I cannot use fixed decimal places i-e i cannot just use 100.10 . I need all decimal places .
Because that is a fundamental part of what happens when you represent an arbitrary floating point value in binary. The number of binary digits is limited, therefore rounding occurs. Depending on your needs, you might be better off using NSDecimalNumber.
Try using double instead;
double temp=100.1;
NSLog(#"%.8f",temp);
100.10000000
It is an issue with representation accuracy. I do not think it will be a problem to use double instead.

Delphi - Comparing float values

I have a function that returns a float value like this:
1.31584870815277
I need a function that returns TRUE comparing the value and the two numbers after the dot.
Example:
if 1.31584870815277 = 1.31 then ShowMessage('same');
Sorry for my english.
Can someone help me? Thanks
Your problem specification is a little vague. For instance, you state that you want to compare the values after the decimal point. In which case that would imply that you wish 1.31 to be considered equal to 2.31.
On top of this, you will need to specify how many decimal places to consider. A number like 1.31 is not representable exactly in binary floating point. Depending on the type you use, the closest representable value could be less than or greater than 1.31.
My guess is that what you wish to do is to use round to nearest, to a specific number of decimal places. You can use the SameValue function from the Math unit for this purpose. In your case you would write:
SameValue(x, y, 0.01)
to test for equality up to a tolerance of 0.01.
This may not be precisely what you are looking for, but then it's clear from your question that you don't yet know exactly what you are looking for. If your needs are specifically related to decimal representation of the values then consider using a decimal type rather than a binary type. In Delphi that would be Currency.
If speed isn't the highest priority, you can use string conversion:
if Copy(1.31584870815277.ToString, 1, 4) = '1.31' then ShowMessage('same');

Resources