News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Floating point numbers

Started by bradyh30, March 31, 2011, 12:30:18 AM

Previous topic - Next topic

bradyh30

Hello All,

I am a new member to the site but i have been searching around here for the past few months, loading up on information. I have just started programming some MASM. The stuff I have done if very rugged and probably not the way it was intended to be done. I am in school and working on a compiler where the output from my C# program is MASM32 asm code. I am not going to ask you to do my homework because i know it is frowned upon around here.

Anyways on to my question. I have a code that uses the FPU to add, subtract, multiply, and divide floating point numbers. I am using real4. I need to print what i have to the command line so at the end of the code i use:

print real4$(floatNum), 13, 10

this gives me the output of say:

1.234000

My question is: Is there any way to get rid of the zeros that follow the number. Or is there a print that i should be using instead of print real4?

Thanks in advance for any help!

qWord

you can use the CRT function sprintf, where you can specific how to print the number:
    print real4$(FP4(12.00)),13,10
   
    ; the msvcrt mostly use double's (real8) - do not use float's (rela4)
    fn crt_sprintf,ADDR sz,"%g",FP8(12.00)
    print ADDR sz,13,10
   
    fn crt_sprintf,ADDR sz,"%.2f",FP8(12.00)
    print ADDR sz,13,10
FPU in a trice: SmplMath
It's that simple!

dedndave

you can convert it to a string and strip off trailing zeros after the DP
the problem that you may see is something like 12.99999999999999   :bg
that could just as well be 13.0, or just 13
what you may want is to fix the number of places after the decimal, as qWord suggests

welcome to the forum   :U

raymond

The FpuFLtoA function in the Fpulib lets you define with one of the parameters the number of decimal places you need in the converted string, the last specified decimal being rounded. If you ever need the decimal point to be aligned for display purposes, that can also be specified.

The function can also convert REAL4, REAL8 or REAL10 as specified by one of the parameters.

If you ever want to learn how such conversion from float to ascii is performed, the library is also available with the source code of each function.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

jj2007

Depending on your problem, you might use Str$(). It works with a fixed overall number of digits:
Quoteinclude \masm32\MasmBasic\MasmBasic.inc   ; download
.data
MyReal4   REAL4   1.234
MyReal8   REAL8   1.234
MyReal10   REAL10   1.2345678901234567890
   Init
   Print Str$
("The number is %4f\n", MyReal4)
   Print Str$("The number is %4f\n", MyReal8)
   Print Str$("The number is %4f\n", 1234/1000)
   Print Str$("The number is %4f\n", 1.234000)
   Print Str$("The number is %Jf\n", MyReal10)
   Inkey "OK"
   Exit
end start


Output:

The number is 1.234
The number is 1.234
The number is 1.234
The number is 1.234
The number is 1.234567890123456789

bradyh30

Cool thank you guys so much. Since I last posted we changed our program completely and now I am having trouble with the floating point stuff again. We decided the way that we were doing stuff was really really nasty looking. We had counters that would increment the name of the variable which, in my understanding, is the only way that you can use float and add and that stuff. It was a mess. Now im kind of thinking of using hex value instead of using floating point numbers. I will search around the site but if anyone has a quick link to a thread with some help i would greatly appreciate it. Thanks again guys!

dedndave

http://www.ray.masmcode.com/

Ray has a great tutorial, as well as an FPU library

bradyh30

Quote from: dedndave on May 03, 2011, 12:03:59 AM
http://www.ray.masmcode.com/

Ray has a great tutorial, as well as an FPU library

Thanks! I have checked out Ray's page in the past. He doesnt do anything with hex. Also, the only way to work with floating point numbers is to put them in a variable which is my problem and why i want to convert to hex. I am trying to avoid the FPU i guess you could say.

dedndave

well - no need to avoid the FPU - it is a powerful and valuable tool

hexidecimal representation is a way for humans to easily interpret binary values
we don't really do much intermediate math in hex, per se
we manipulate binary values
sometimes, we then convert the binary result to ascii hex for display purposes

what you really want to do is work with binary
a register holds 32 bits - that can represent either a signed or an unsigned value

when you are done with the intermediate calculations, you can easily include the integer into FPU calculations
because the FPU has load integer and store integer instructions made just for that purpose

the numeric data types you use depend entirely on what you want to accomplish   :P
maybe you could give us a simple, hypothetical example of where you are going

bradyh30

Quote from: dedndave on May 03, 2011, 12:31:07 AM
well - no need to avoid the FPU - it is a powerful and valuable tool

hexidecimal representation is a way for humans to easily interpret binary values
we don't really do much intermediate math in hex, per se
we manipulate binary values
sometimes, we then convert the binary result to ascii hex for display purposes

what you really want to do is work with binary
a register holds 32 bits - that can represent either a signed or an unsigned value

when you are done with the intermediate calculations, you can easily include the integer into FPU calculations
because the FPU has load integer and store integer instructions made just for that purpose

the numeric data types you use depend entirely on what you want to accomplish   :P
maybe you could give us a simple, hypothetical example of where you are going

So what we are doing is writing a compiler for a mock language. A person can make and arbitrary number of operations in an expression:

5 + 3  + 2 + ..... and the same with floating point numbers.

and then they can have an arbitrary number of expressions. I have no idea how many this is going to be so i have to some how, in C# (because that is the language we are writing it in), output this assembly so that it will compile and run correctly. Float is the only thing i am having trouble with.

dedndave

well - a register (or a dword in memory) can hold 32 bits, as i said
signed range: -2,147,483,648 to 2,147,483,647
unsigned range: 0 to 4,294,967,295

how big you want ?

bradyh30

I dont need anything bigger than a DWORD but it is the arbitrary number of variables that is my problem. Or i suppose a function that i call with a couple parameters that add the floating point number. hmmm im not good with functions.

dedndave

ok
i think i understand your problem   :P

one way to handle it is to create a variable length structure or array
rather than passing the values as the parameter(s), pass the address (pointer) of the structure or array
the first element of the structure can be a variable that tells the routine how many items are in the list

bradyh30

Quote from: dedndave on May 03, 2011, 01:03:43 AM
ok
i think i understand your problem   :P

one way to handle it is to create a variable length structure or array
rather than passing the values as the parameter(s), pass the address (pointer) of the structure or array
the first element of the structure can be a variable that tells the routine how many items are in the list

That is a good idea. Is there a tutorial on that kind of thing? I have hardly any experience with assembly and especially that kind of thing.
I think what you are saying is i can make a routine that loop to create my variables and their values?

raymond

Many questions now that we know what you are trying to accomplish.

1. Do you intend to use C# operations for parsing the expression?
2. Do you intend to use C# functions to convert the numerical values of the expression from ascii to binary (integers and floats) before performing the computations?
3. Can you use in-line assembly with C#?
4. Can you use assembly libraries with C#?
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com