2

like in C and C++

double x

How to do it in ruby ?

Or to be in the type float?

I want to run

x=5/2

then

x=2.5

instead of

x=2
mewosic
  • 465
  • 2
  • 7
  • 11
  • 1
    I think the answers you're lokking for are in this post : http://stackoverflow.com/questions/5502761/why-is-division-in-ruby-returning-an-integer-floating-point-arithmetic – Laurent S. May 15 '13 at 11:27
  • @Bartdude I am not that boring to answer a repeated question. I cannot find it ! F – mewosic May 15 '13 at 11:33
  • When you write `5/2` in C++ it will be still `2`. Nothing changed. – Hauleth May 15 '13 at 11:58

5 Answers5

9

If at least one of the operands is a float, the result will be a float too.

5 / 2.0 # => 2.5
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
5

In ruby, you create a float by specifying a decimal point:

2 #=> integer
2.0 #=> float

If you divide an integer by another integer, you get an integer. You have to use a float in the division:

5 / 2   #=> 2
5.0 / 2 #=> 2.5
5 / 2.0 #=> 2.5
Jon Cairns
  • 11,783
  • 4
  • 39
  • 66
3

Ruby is dynamically typed, so there isn't an explicit way to cast a variable to a specific type. Eg:

a = "a" # here a is a String
a = 5   # now a is a Fixnum
a = 5.0 # now a is a Float

is totally valid.

So what happens when you say 5/2? Ruby looks up the / operator in the first operand, in this case an integer, and then calls the function with the second operand, also an integer. Thus Ruby assumes you want integer division.

The solution is to make one of the operators a float, this can be done in at least two ways:

5 / 2.0

or

5 / 2.to_f

jbr
  • 6,198
  • 3
  • 30
  • 42
1

Variables don't have types in Ruby, only objects do. (And the concept of "type" is latent, it is not manifest in the program. The "type" of an object is the protocol it speaks, i.e. the messages it responds to and how it responds to those messages. In particular, the type of an object is not its class.)

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Type is a class with methods, so why are you paraphrasing the simple and well understood concept of "class" via queuing theory? I think your answer is just verbal salad. – nurettin May 15 '13 at 12:07
  • @nurettin: Two objects which speak the same protocol have the same type, *regardless* of whether they have the same class. That's a *fundamental* property of object-orientation. It's the single property that distinguishes it from other forms of abstraction, e.g. Abstract Data Types. Without that property, there is no difference between object-oriented data abstraction and ADTs. In Ruby, there is no way to actually talk about this type, there is no notion of protocol or type in Ruby. In Java or C#, there are `interface`s, which attempt to capture the protocol, but usually fail in some way. – Jörg W Mittag May 15 '13 at 12:34
  • I'm not disagreeing with the first sentence in your answer. But what interface are you typing about? 2 and 5 both share the same class. The notion of class doesn't have to exist at compile time or inherit some "interface protocol agreed by both parties" in order to be talked about. `5.is_a? Fixnum` and that's it. I think you make sense and then go off on a tangent trying to write a unifying theory. – nurettin May 15 '13 at 13:48
  • While this is true, it doesn't answer the question. It would have been better as a comment on the question itself. – Jon Cairns May 15 '13 at 14:04
0

By default in ruby all the floats are double according to the source code

To create a float value you have to add .0 to difference from the integers value.

Note: if you use two integers in a one operation by default the result is an integer

5.0 # float
5   # integer
5 / 2 = 2 # integer
5 / 2.0 = 2.5 #float

Float definition on the ruby source code

enter image description here

Joel Ibaceta
  • 236
  • 3
  • 15