Dummy float values to force floating-point operations

It is common to use idioms such as:

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

to force a floating-point division when x is an integer in languages which do not have distinct operators for integer and decimal division.

Is this an accepted idiom or is it preferrable to cast x to a floating-point type?

I believe it is a concise and clear idiom- it might be show intent less clearly than an explicit cast, but it is more concise and easier to read. I think that it’s less hack-ish than x + "" to convert to string or x+0.0 to convert to floating point (as there’s no unnecessary “NOOP”).

Thoughts?

Álex

1

The return result will be language specific based upon how the language handles implicit conversions.

That having been said, if your language of choice will support returning a float from that implicit conversion, then yes, your example is a pretty common way of triggering the float point arithmetic. It’s quick, clean, and clear.

Some languages will require the explicit cast, so the norm in those cases would obviously be the explicit cast.

Update:
Alex asked:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
would you prefer x/60.0 or float(x)/60? Why?
would you prefer x/60.0 or float(x)/60? Why?
would you prefer x/60.0 or float(x)/60? Why?

And my preference is for the first, as this example will explain. Please note that my comments are still targeted to languages that allow the implicit conversion. Languages that require an explicit conversion are out of scope for the answer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = 65;
float y = 0.0;
y = x / 10.0; //will yield 6.5
int x = 65; float y = 0.0; y = x / 10.0; //will yield 6.5
int x = 65;
float y = 0.0;

y = x / 10.0;  //will yield 6.5

vs.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = 65;
float y = 0.0;
y = (float) x / 10.0; //will still yield 6.5
int x = 65; float y = 0.0; y = (float) x / 10.0; //will still yield 6.5
int x = 65;
float y = 0.0;

y = (float) x / 10.0;  //will still yield 6.5

Why?

  • The implicit cast is what I would “expect” the code to be doing in the first place. When I’m mentally performing the calculations prior to typing them out, my mind switches between Integers and Reals without worrying about it. In this case, I expect the compiler to be able to do the same.
  • The explicit cast muddies up the code. Reading it out loud, I get “y equals float x divided by 10” instead of just “y equals x divided by 10.” Inserting the explicit cast interrupts my pattern of thought while reading the code.
  • And while this may just be my guilty conscience, I always feel like I’m doing something wrong when I require an explicit cast. I feel like I’m telling the compiler “You wouldn’t normally do this because it’s wrong, but I swear to you it’s okay in this case. Really, it is, just believe me, ok?” (I agree, I could use some therapy on the issue). The compiler should be smart enough to allow the conversion or throw an error when there is data loss.
  • Finally, when adding an unnecessary explicit cast there is a chance for introducing a bug later on in the code’s life. If one of the variable types changes, the explicit cast may allow something to compile that really should not have. Granted, this is pretty rare but it can be very difficult to hunt down.

4

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