The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: bradyh30 on March 31, 2011, 12:30:18 AM

Title: Floating point numbers
Post by: bradyh30 on March 31, 2011, 12:30:18 AM
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!
Title: Re: Floating point numbers
Post by: qWord on March 31, 2011, 01:00:16 AM
you can use the CRT function sprintf (http://msdn.microsoft.com/en-us/library/ybk95axf(v=vs.71).aspx), where you can specific (http://msdn.microsoft.com/en-us/library/56e442dc(v=vs.71).aspx) 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
Title: Re: Floating point numbers
Post by: dedndave on March 31, 2011, 01:11:17 AM
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
Title: Re: Floating point numbers
Post by: raymond on March 31, 2011, 02:33:02 AM
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.
Title: Re: Floating point numbers
Post by: jj2007 on March 31, 2011, 11:33:29 AM
Depending on your problem, you might use Str$(). It works with a fixed overall number of digits:
Quoteinclude \masm32\MasmBasic\MasmBasic.inc   ; download (http://www.masm32.com/board/index.php?topic=12460)
.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
Title: Re: Floating point numbers
Post by: bradyh30 on May 02, 2011, 11:30:42 PM
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!
Title: Re: Floating point numbers
Post by: 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
Title: Re: Floating point numbers
Post by: bradyh30 on May 03, 2011, 12:26:02 AM
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.
Title: Re: Floating point numbers
Post by: 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
Title: Re: Floating point numbers
Post by: bradyh30 on May 03, 2011, 12:47:39 AM
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.
Title: Re: Floating point numbers
Post by: dedndave on May 03, 2011, 12:51:56 AM
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 ?
Title: Re: Floating point numbers
Post by: bradyh30 on May 03, 2011, 12:58:00 AM
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.
Title: Re: Floating point numbers
Post by: 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
Title: Re: Floating point numbers
Post by: bradyh30 on May 03, 2011, 01:20:13 AM
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?
Title: Re: Floating point numbers
Post by: raymond on May 03, 2011, 01:36:08 AM
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#?
Title: Re: Floating point numbers
Post by: bradyh30 on May 04, 2011, 12:35:15 AM
Quote from: raymond on May 03, 2011, 01:36:08 AM
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#?


Hi Raymond I hear lots of good things about you. Also, thanks for your great FPU tutorial. Now to answer your questions:
1. Yes, we have already implemented this.
2. Yes, i am currently working on this.
3. Not that I know of.
4. I believe they have some but I have never used any.
Title: Re: Floating point numbers
Post by: qWord on May 04, 2011, 12:46:52 AM
Quote from: bradyh30 on May 04, 2011, 12:35:15 AM
4. I believe they have some but I have never used any.
c# allows to use any libray in form of an DLL.
Title: Re: Floating point numbers
Post by: bradyh30 on May 04, 2011, 01:08:12 AM
Yeah anyone can write a library for anything i guess.

So i will change my answer to I'm sure there is one out there but I have never used one.
Title: Re: Floating point numbers
Post by: raymond on May 04, 2011, 01:56:27 AM
From ypour first post:
QuoteI have a code that uses the FPU

Then:
Quote3. Can you use in-line assembly with C#?
3. Not that I know of.

What would be the kind of code where you can "use the FPU"?
As it is, I can only see inherent C# functions, to which you have no direct access, that would use the FPU to perform floating point computations.
Title: Re: Floating point numbers
Post by: bradyh30 on May 04, 2011, 02:18:37 AM
Quote from: raymond on May 04, 2011, 01:56:27 AM
What would be the kind of code where you can "use the FPU"?
As it is, I can only see inherent C# functions, to which you have no direct access, that would use the FPU to perform floating point computations.

Well the compiler is suppose to take floating point numbers. so within C# we are checking to see if it is float then adding, subtracting, ... is done withing the assembly itself. so that is where we are actually using the FPU.

Sorry if that didn't answer your question.
Title: Re: Floating point numbers
Post by: raymond on May 04, 2011, 03:07:44 AM
If you're expecting a possible mixture of floats and integers, I don't know if it's possible in C# but the easiest way may be to treat all input as floats even if it doesn't contain a decimal delimiter; or convert all integers immediately to floats before storage. If you're not concerned with precision and the available range is satisfactory, you can keep all of them in 32-bit variables. It's a lot easier to deal with a single type of data once you start computing the expression.

However, if the expression contains shifts or boolean operators, you would need to keep the input as integers!!! :eek Part of the fun of trying to create a compiler for a new programming language. ::)

As for displaying data without trailing 0's, you may simply have to parse the outgoing result to remove them before you display it.
Title: Re: Floating point numbers
Post by: bradyh30 on May 04, 2011, 03:33:13 AM
Quote from: raymond on May 04, 2011, 03:07:44 AM
If you're expecting a possible mixture of floats and integers, I don't know if it's possible in C# but the easiest way may be to treat all input as floats even if it doesn't contain a decimal delimiter; or convert all integers immediately to floats before storage. If you're not concerned with precision and the available range is satisfactory, you can keep all of them in 32-bit variables. It's a lot easier to deal with a single type of data once you start computing the expression.

However, if the expression contains shifts or boolean operators, you would need to keep the input as integers!!! :eek Part of the fun of trying to create a compiler for a new programming language. ::)

As for displaying data without trailing 0's, you may simply have to parse the outgoing result to remove them before you display it.

Thanks Raymond! I am going to do it the dirty way where i just loop through the number of floats I need and assign their values. I am not going to do anything else with this compiler when I am done with the class so it doesn't matter much to me. Sounds kind of bad I guess. I will deal with the output with trailing 0's when I finally get some output  :U