Who is this for?

This is an advanced topic for C/C++ developers who are interested in learning about the intricacies of conversions between floating-point numbers and integers.

What will you learn?

Upon completion of this Learning Path, you will be able to:

  • Learn how to identify and fix potential problems in integer/float conversions in C/C++ on Arm

Prerequisites

Before starting, you will need the following:

  • An Arm computer running Linux and a recent version of a C++ compiler (Clang or GCC) installed

Summary

AI-assisted

This summary was drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.

Close
?
You’ll explore how C and C++ convert integers and floating-point values on Arm Linux, and how those conversions affect correctness. You’ll review numeric ranges, compare explicit and implicit conversions, and run short programs that expose integer-division and narrowing pitfalls. You’ll also compare C++ list-initialization diagnostics with C’s permissive behavior, then adjust types or add explicit casts to produce the intended results.

Frequently asked questions

AI-assisted

These FAQs were drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.

Close
?
How do I build the examples?
Compile the C source as C and the C++ source as C++ with a recent GCC or Clang on your Arm Linux system. Run the binaries and compare their output. A narrowing diagnostic from the C++ brace-initialization example is expected.
What result should I expect from the golden ratio program?
The ratio approaches 1.6180339887… as the Fibonacci index increases. If the output stays at whole numbers or fails to converge, check for integer division and promote one operand to a floating-point type.
How do I know an implicit conversion is changing my calculation?
Inspect the operand types in each expression. Two integer operands produce integer arithmetic, so promote one value or cast it to float or double when you need a fractional result.
Why does the C++ demotion example behave differently with braces?
C++ list initialization with braces can diagnose narrowing conversions at compile time, so float z{w} can produce a diagnostic. An assignment such as float y = w usually compiles and truncates the value at runtime.
What should I change if the demotion test prints unexpected values?
Use a wider destination type for intermediate results when you need to preserve range and precision. Add an explicit conversion only when truncation is intentional, then rebuild and recheck the output.
Next