0

Is there any way to output the floating point number to the sixth decimal place in assembly? How to write? I use Irvine32.

The general output is +x.xxxxxxxE+xxx

I know the x87 control word can set rounding. But I don't how to write.

Example:
I want to show +2.123456.
General output is +2.1234562E+000.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
lingwu
  • 41
  • 1
  • 3
  • The x87 control is not relevant. You want to round to a certain number of *decimal* places, but the x87 control word (http://www.ray.masmcode.com/tutorial/fpuchap1.htm#cword) only controls rounding of the mantissa to a given number of *binary* bits. 2.123456 is not exactly representable as a `float` or `double` (the fractional part's denominator isn't a power of 2 if written as `123456 / 100000` and simplifying the fraction), so **the rounding to a given number of decimal digits has to be done by software while it's converting a number to a string**. – Peter Cordes Jun 11 '20 at 09:27
  • Does Irvine32 have any float / double input or output functions? I only see integer and string in/out functions like [`WriteDec`](http://csc.csudh.edu/mmccullough/asm/help/index.html?page=source%2Firvinelib%2Fwritedec.htm). So you might just use `printf` from the C library (using a different calling convention than Irvine functions: [Read and display float or double number in MSVC](//stackoverflow.com/q/40930608)). It has powerful formatting options, like `%.6f` or `%.6g` ("general"). https://en.cppreference.com/w/c/io/fprintf has detailed docs, and some examples at the bottom. – Peter Cordes Jun 11 '20 at 09:33
  • Or if can't use any library functions to format FP numbers into strings, you'd have to do it yourself. It's harder than you'd expect to do it exactly correct without rounding errors for very large or very small numbers. e.g. [Turn float into string](https://stackoverflow.com/q/17632150) – Peter Cordes Jun 11 '20 at 09:40
  • [How to display floating-point rounded to .001 with Irvine32 WriteFloat, not printf](https://stackoverflow.com/q/61020815) mentions WriteFloat. Can you just use that? It rounds to 6 places, doesn't it? Anyway, the answer there shows a way you can use integer stuff to get the output you want. – Peter Cordes Jun 11 '20 at 09:41

0 Answers0