Performance and other issues with using floating point types in C++

Being interested in C++ performance programming there is one aspect I really have no clue about- and that is the implications of using floating point calculations vs doubles vs normal integer mathematics?

What performance issues with these types of calculations should I consider? What other issues need to be considered when deciding which kind of operators to use?

All I currently know is that doubles are probably the most expensive because they are larger, floating point is next and then integer calculations are usually the fastest on a CPU because they are much simpler.

9

The comments by @ratchet, @Sjoerd and @Stephane answer you question.

Your assertion in the question “All I currently know is that doubles are probably the most expensive because they are larger” shows the rules about optimization – are true – “Only for experts” followed by the “Your not an expert yet” …..

Unless you know the minutest details of the underlying hardware AND how the compiler utilizes those, you cannot make any assertions about floating point numbers. You can’t even be certain that a floating point operation takes more time than an integer operation.
As a rule, there is enough problems with programmers writing floating point code correctly, that it should be avoided unless needed. If needed, performance is of little concern.

As a rule of thumb – use ints if possible – it’s rarely slower then FP operations, and often faster, and more predictable. If you must use FP operations, use doubles. Floats do not have a large enough mantissa for anything but the roughest of calculations (unless extreme care is taken) and a prone to insidious rounding errors.

However, like all rules of thumb, they need to be applied appropriately. If it matters – measure it.

1

Use the Right Tool for the Job

Remember the old saw about the man who only had a hammer?

There are many integer and many floating point types and there are many implementations for them. If you get to know these well, it will make your results better, not to mention your code. When you make your choices, you will use several criteria.

Application

If your application basically uses whole numbers, generally use integers. In C++ you have both signed (the default) and unsigned, and selecting unsigned will not only give you almost twice the maximum limit, it may permit the compiler to warn you about unwise uses of your number.

If it uses decimal values, often a floating point representation may be better. First, it may be more natural to program an equation from science, engineering, or business when the values are not restricted to whole numbers. Binary coded decimal (BCD) can also help with currency and although it takes more space and is slower, it has some desirable characteristics when used appropriately.

If you application relates to currency, that might be a tricky one because floating point is not precise, so occasionally you can gain or lose cents due to rounding. Sometimes you can manipulate in integer cents instead of floating point dollars to speed things up and make them more precise.

Prefer Doubles to Floats

I generally prefer doubles to floats because they are less likely to introduce rounding errors. The C++ compiler will often promote floats to doubles as it passes them to standard library functions even for calculations with basic operators. Using doubles generally makes your code and the compiler’s generated machine code a little bit less complicated.

Rounding very frequently can become a concern, so you will need to consider conversions between integer and floating point, sequence of evaluation within equations, accumulated errors within loops, and normalization of very large and very small values when used together. You will wish to validate the ranges of both inputs and outputs, and sometimes intermediate values to insure things have not gone wrong.

Size Matters

To insure your results have precise values, you may need to understand the range and representation of the floating point types, and a little bit of numerical analysis. http://en.wikipedia.org/wiki/Floating_point has a lot of cool information.

As another down vote for floats in favor of doubles consider that a 32-bit float will burn eight bits for the exponent, and the mantissa part that holds a scaled version of your value will need to fit in 23 bits after you account for the sign of the mantissa. 2^23 is not particularly big. If you want to count from one to 10 million, you might want to use a 32 bit integer or a double.

Underlying architecture

If you have hardware floating point, generally it is OK to use it. If you don’t, highly optimized libraries on fast clocked fix point processors may still cope very well.

If you have a eight bit embedded work horse, learn how to scale and normalize inputs and by all means, carefully implement your repetitious and time consuming calculations using fixed point. I am an Arduino documentation fan, even though my taste is to use beefier hardware when I work on embedded systems. The following link has a good discussion of what to do when your environment doesn’t have native floating point and your compiler only gives you floats (but no doubles).

http://www.sparkfun.com/tutorials/317

Many fixed point oriented architectures (for example, some Digital Signal Processors (DSP)) can help you with hardware instructions like multiply – accumulate, and often include very capable instructions to help multiply or do arithmetic shifts using integers. Details for using these may be performed by the compiler or by libraries, so it pays to learn as much as you can about your processor, particularly if you work on DSP or embedded projects.

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