flat assembler
Message board for the users of flat assembler.

 Index > DOS > extract the integer part of the real number Goto page 1, 2  Next
Author
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
i want to extract the integer portion of a floating point number.
for example
4789640 of 4789640.889
i made following programme(attached with this)but it gives 4789641(due to rounding)

is there a way to get exact value of the integer part?

04 Nov 2015, 11:58
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17671
revolution
I would expect a simple method to be setting the FPU rounding mode to truncate and then use FIST.
04 Nov 2015, 12:11
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thank you for the reply.in this i needed out put as bcd number that's why i used fbstp .
infact i have set truncate mode in this, before round it to integer.
thanks.
04 Nov 2015, 13:14
AsmGuru62

Joined: 28 Jan 2004
Posts: 1419
AsmGuru62
@mns:
Are you trying to make FPU number into a string?
04 Nov 2015, 15:12
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
yes AsmGuru62. integer part first (separately) and then multiply the decimal portion by 10^power and extract it as a integer separately.that's why i needed to extract integer as the exact number without rounding(changing it).
04 Nov 2015, 15:43
AsmGuru62

Joined: 28 Jan 2004
Posts: 1419
AsmGuru62
If the values you need to display are no longer than 18 digits, then you still can use FBSTP.
For example, assume that ST0 = 62716711.93830029882 and you need to display
this value with precision of 8 digits after the decimal point. Then following steps may be taken:

1. Multiply ST0 by 10^8 (8 digits) -> you will get: 6271671193830029.882
2. Use FBSTP -> you will get 6271671193830030 as BCD
3. Insert a decimal point at offset -8 counting from end of the value -> you will get "62716711.93830030"

This however will not work for a larger values, like 6272822977339829327238.633728282
Also, will not work for a small values, like 0.0000000000000000000000000000000004637382

For these cases (to display the values) you will need to get mantissa/exponent bits and use different methods.
If you search the forum: there was quite a bit of routines for what you need (they may be 32-bit code, but you can translate into 16-bit).
04 Nov 2015, 18:00
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thanx AsmGuru62. but i need is getting the integer part without changing to nearest number(due to rounding).for example in 4789640.889
4789640 part (as it is) not as 4789641(as in my method as well as yours will give)
04 Nov 2015, 19:33
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17671
revolution
So use FIST. Don't use FBSTP. Use some integer code to generate the ASCII after you have the integer portion you want. Did I mention that you can use FIST?

Edit:

You could even use FIST and then FILD and then FBSTP.

Or maybe FRNDINT and then FBSTP.

There are many options, but all require the FPU to be in truncate mode.

If you don't use the FPU then using the integer core can also solve this for you, but would require more coding.
04 Nov 2015, 22:24
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thanks revolution.
i've used FIST (source attached here with) but the result is same as FBSTP (even after setting to the truncate mode).
also i've used FBSTP after FRNDINT(in truncate mode) -->same result.

05 Nov 2015, 15:48
AsmGuru62

Joined: 28 Jan 2004
Posts: 1419
AsmGuru62
The code from 1st post works fine.
I just tried in in debugger and the value is "+4789640" and not "+4789641".
05 Nov 2015, 18:40
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thanks AsmGuru62.
but i tried with debug.exe (i'm only having lower basic knowledge about the debugging as well)and 't' command.in the last ax=0041(not 0040).
i'm doing these in windows xp guest in windows 10(host) with virtual box.can it be the reason for this different result of mine?
06 Nov 2015, 03:20
AsmGuru62

Joined: 28 Jan 2004
Posts: 1419
AsmGuru62
It should not be the cause for it.
I will try your code in DOS Box .74 on Win7 x64.
I have no VMs here.

Curious, it prints "+4789641" in DOS Box.
I guess, something else is going on.
Turbo Debugger will help.

Debugger is a king, of course!
The issue is that DWORD used to represent value of 4789640.889 has not enough precision. It is only 4 bytes long and FPU can deal with 8-byte or 10-byte values.

So, you need to make this change:

Code:
``` float1     dd  4789640.889
```

into:

Code:
``` float1     dq  4789640.889
```

And now it prints the "+4789640".
06 Nov 2015, 11:44
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thank you very much AsmGuru62.it worked like a charm.
07 Nov 2015, 04:21
fasmnewbie

Joined: 01 Mar 2011
Posts: 555
fasmnewbie
mns

Your original code is logically correct but is technically incorrect. If you are dealing with a float (32-bit), you get only 24-bit precision, that means you have only (approx) 7 digits to represent your data. You current data has exponent of 22. That leaves only 2 bits for the FPU to decide your decimal point and it is not enough to represent anything useful. So FPU decided to round up your data regardless of rounding mode or frndint. In order for your code to display correctly, you must wrap it up in exponent notation once it reaches certain exponent (> 19). There must be a limit on everything my friend

The solution by AsmGuru is just a temporary solution based on your current code and your current approach. If you want to stick with 32-bit floats, then all you have to do is to count your digits and once it has reached 7 you need to stop parsing it. But b4 that, you need to come up with a solution on how to deal with very big (your current data is considered 'big' as per float) or very small number because they require Exponent notation wrap-up or else your program will hang or produce funny result.

But I admire your spirit. Keep up the good work. You'll get it in no time.
07 Nov 2015, 14:01
fasmnewbie

Joined: 01 Mar 2011
Posts: 555
fasmnewbie
Btw, I've read in other forums / sites that parsing float to string is considered 'crazy', uncivilized, low-lives, almost impossible, extremely difficult, better use C... so on so forth. Well, what do you know, FASM novices are taking float-to-string parsing for breakfast!

Way to go FASM! Full of weird people! xD
07 Nov 2015, 14:41
HaHaAnonymous

Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
Quote:

Btw, I've read in other forums / sites that parsing float to string is considered 'crazy', uncivilized, low-lives, almost impossible, extremely difficult, better use C.

That is lazy mentality, in my opinion.

Last edited by HaHaAnonymous on 11 Nov 2015, 14:16; edited 1 time in total
07 Nov 2015, 15:02
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thank you fasmnewbie for your valuable insights.
by the way i thought in c it's compiler first convert the source to assembly code before generating the machine code.if that's correct that mean if we use c, we use someone's assembly routines(no offence for c programmers) over and over again without understanding really what's happening?
actually, i like c also and trying to learn it in these days side by side with assembly.
07 Nov 2015, 16:55
HaHaAnonymous

Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
Quote:

by the way i thought in c it's compiler first convert the source to assembly code before generating the machine code.

Yes, the machine code is generated by the assembler.

Quote:

if that's correct that mean if we use c, we use someone's assembly routines(no offence for c programmers) over and over again without understanding really what's happening?

Yes. And the things get worse with object oriented programming.

Quote:

actually, i like c also and trying to learn it in these days side by side with assembly.

The main advantage of compiler is portability across multiple operating systems and architectures. And that comes with the price of increased risk of bugs and errors.

Just my opinion, don't take anything as real without validating first. :P
07 Nov 2015, 17:10
mns

Joined: 20 Dec 2007
Posts: 121
Location: Piliyandala,Sri lanka
mns
thanks HaHaAnonymous.
also less work ,less time consuming and less freedom.
07 Nov 2015, 19:23
fasmnewbie

Joined: 01 Mar 2011
Posts: 555
fasmnewbie
HaHaAnonymous wrote:
Quote:

Btw, I've read in other forums / sites that parsing float to string is considered 'crazy', uncivilized, low-lives, almost impossible, extremely difficult, better use C.

That is lazy mentality, in my opinion.

I think that's because they don't share FASM spirit. The creation of FASM (by King Tomasz) inspires me to be more independant (self-hosting, self-compiling and all those philosophical stuff etc), less reliant on external thingy, and I think FASM programmers should be inspired that way too. And I am glad to see that FASM novices like mns is demonstrating that spirit.
08 Nov 2015, 07:48
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area
Goto page 1, 2  Next

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum