Paul Krill
Editor at Large

Rust language adds algebraic floating-point methods

news
Aug 28, 20263 mins

The new algebraic methods allow optimizations on floating-point addition, subtraction, multiplication, division, and remainder operations using the algebraic properties of real numbers.

shutterstock 1912506373 scattered wooden tiles with numbers and signs
Credit: Natalya Duryagina / Shutterstock

The Rust team has released Rust 1.98.0, an update to the language that introduces algebraic methods for floating-point operations.

Developers who have a previous version of Rust installed via rustup can update to Rust 1.98.0 by running rustup update stable.

Introduced August 20, Rust 1.98.0 brings algebraic methods for addition, subtraction, multiplication, division, and remainder to the floating-point types f32 and f64. The algebraic methods allow optimizations on these operations using algebraic properties of real numbers. These optimizations are not specified, but may be similar to the kind of optimization seen with the -ffast-math option in other languages, the Rust team said.

“For example, floating-point addition is not associative, so a sum like a + b + c + d must be evaluated in the left-associative order in which it is parsed, like ((a + b) + c) + d,” the Rust team noted. “If you write the same sum as a chain of algebraic_add calls, then the compiler is free to reorder it, perhaps like (a + b) + (c + d) to evaluate the partial sums simultaneously. Broader loop-vectorization is often enabled by using these algebraic methods as well.”

The methods are non-deterministic, because the compiler is free to choose different optimizations, but they never cause undefined behavior, the Rust team said.

Rust 1.98.0 also introduces buffered integer formatting. All primitive integer types now have a format_into method that takes a &mut NumBuffer<Self> parameter, which is a buffer large enough to hold the decimal format of any value of the type. The format_into method returns the formatted &str with a lifetime borrowed from the buffer, and bypasses much of the dynamic dispatch of buffered write! formatting, improving performance.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorld’s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorld’s audience of software developers and other information technology professionals. Paul has won a “Best Technology News Coverage” award from IDG.

More from this author