Is there a programming language where 1/6 behaves the same as 1.0/6.0?

While I was programming in C++ some days ago, I made this mistake (that I have history of making it!). In one part of my code, I had 1/6 and I was expecting it be 0.16666666666 which is not the case. As you all know the result is 0 – C, C++, Java, Python, all behave the same.

I post it on my Facebook page and now there is debate on if there is a programming language where 1/6 behaves the same as 1.0/6.0.

5

Has everyone forgotten Pascal?

1/6 yields 0.1666666... (to whatever precision is supported).

1 div 6 yields 0

It’s arguable whether the C rule is a mistake. Almost all of C’s arithmetic operators, where the operands are of the same type, yield a result of the same type. There’s something to be said for consistency.

Furthermore, since C is primarily targeted at system-level code, most C programs don’t use floating-point at all. At one time, accidentally adding floating-point code to a program that didn’t otherwise need it could be a serious problem. That’s probably still the case, for small embedded systems — which, again, are a major target for C.

In most C programs, truncating integer division is probably just what you want anyway.

If 1 / 6 yielded a floating-point result in C, then:

  • It would be an inconsistency in the language.
  • The standard would have to make an arbitrary choice of which floating-point type to use for the result (double may seem like the natural choice, but you might prefer the extra precision of long double)
  • The language would still have to have an operation for integer division; performing floating-point addition and then truncating would likely not be good enough.

C could have provided separate operators for the two kinds of division, but the second point above would still apply: which of the three floating-point types would be used for the result? And since it’s easy enough to get floating-point division if you need it (use a floating-point constant for one or both of the operands, or cast one or both of the operands to a floating-point type), it apparently wasn’t considered that important.

In the 1974 version of the C manual (that’s 4 years before the publication of the first edition of K&R), Ritchie doesn’t even mention the possible confusion:

The binary / operator indicates division. The same type considerations as for multiplication apply

which says that if both operands are of type int or char, the result is of type int.

Yes, it’s a source of confusion for some C programmers, especially beginners — but C is not noted for being very novice-friendly.

4

Actually this behavior was changed in Python 3 and it now does behave like you expect (// is now used for integer division).

2

Out of prominent languages, JavaScript. 1.0/6.0 = 1/6 =
0.16666666666666666.

I don’t see this as surprising. As a rule of a thumb, if a language distinguishes between integer and floating point numeric types, dividing two integers will yield a truncated integer instead of float. If it doesn’t, most likely it will default to floating point operations. This should be the expected behaviour on the programmer’s part.

Just keep in mind that there are additional things that could also be at play here, like the already mentioned separate integer division operator or implicit type casting.

16

There are many languages where ((1/6)*6) results in 1, not in 0. For example, PL/SQL, many BASIC dialects, Lua.

Accidentally, in all those langauges 1/6 results in .166666667, or 0.16666666666667 or something similar. I chose the ((1/6)*6)==1 variant to get rid of those little differences.

9

Haskell treats 1/6 and 1.0/6.0 as being identically 0.16666666666666666. It also renders 1/6.0 and 1.0/6 as being that same value as well.

This is due to the basic numerical types in Haskell not being quite the same as other languages. True integer division is somewhat…complicated.

Yes, Perl does. The one-liner

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>perl -e '$x=1/6;print "$xn";'
</code>
<code>perl -e '$x=1/6;print "$xn";' </code>
perl -e '$x=1/6;print "$xn";'

results in the output of:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>0.166666666666667
</code>
<code>0.166666666666667 </code>
0.166666666666667

I believe that PHP works the same way.

Edited to add: I also believe that a necessary (but not sufficient) condition for 1/6 == 1.0/6.0 is for the language in question to be weakly typed.

8

In Squeak Smalltalk / on integers creates Fraction objects. So while this is not the same as float division, still (1/6)*6 returns 1.

1

Yes, I just checked my TI-99/4A’s built in TI BASIC. As it treats all numeric expressions as floating point, the division operation is floating point as well.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> TI BASIC READY
>PRINT 1/6
.1666666667
>
</code>
<code> TI BASIC READY >PRINT 1/6 .1666666667 > </code>
 TI BASIC READY
>PRINT 1/6
  .1666666667

>

VB (VB.Net , VB6, VBA…)

The integer division operator is

MATLAB. Numeric literals are doubles by default.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>>> 1/6
ans =
0.1667
</code>
<code>>> 1/6 ans = 0.1667 </code>
>> 1/6
ans =
    0.1667

Clojure does use fractions by default. It’s not the same as 1.0/6.0, but you can convert to it with float or double when you need.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>user=> (/ 1 6)
1/6
user=> (* (/ 1 6) 2)
1/3
user=> (pos? (/ 1 6)) ; Is 1/6 > 0?
true
user=> (float (/ 1 6))
0.16666667
</code>
<code>user=> (/ 1 6) 1/6 user=> (* (/ 1 6) 2) 1/3 user=> (pos? (/ 1 6)) ; Is 1/6 > 0? true user=> (float (/ 1 6)) 0.16666667 </code>
user=> (/ 1 6)
1/6
user=> (* (/ 1 6) 2)
1/3
user=> (pos? (/ 1 6)) ; Is 1/6 > 0?
true
user=> (float (/ 1 6))
0.16666667

Surprisingly, it seems to work properly in Windows PowerShell (version 3).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>PS C:> 1.0 / 6.0
0.166666666666667
PS C:> 1/6
0.166666666666667
</code>
<code>PS C:> 1.0 / 6.0 0.166666666666667 PS C:> 1/6 0.166666666666667 </code>
PS C:> 1.0 / 6.0
0.166666666666667

PS C:> 1/6
0.166666666666667

Also seems to work in Python 3 as sepp2k mentioned. The other two languages I have readily available on REPL, Scala and Ruby, both do integer division and yield 0.

The Rexx language always produces an arithmetically correct answer. For example: 5/2 = 2.5. Rexx is a great language that has not been utilized enough. In theory, when a compiler can’t determine what do you want, its better to do the correct math, however, this may not be efficient. Rexx also provides the // operator.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật