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
Thread Post new topic Reply to topic
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 04 Nov 2015, 11:58
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?


Description:
Download
Filename: WrtNum5.ASM
Filesize: 3.51 KB
Downloaded: 853 Time(s)

Post 04 Nov 2015, 11:58
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 04 Nov 2015, 12:11
I would expect a simple method to be setting the FPU rounding mode to truncate and then use FIST.
Post 04 Nov 2015, 12:11
View user's profile Send private message Visit poster's website Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 04 Nov 2015, 13:14
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.
Post 04 Nov 2015, 13:14
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1631
Location: Toronto, Canada
AsmGuru62 04 Nov 2015, 15:12
@mns:
Are you trying to make FPU number into a string?
Post 04 Nov 2015, 15:12
View user's profile Send private message Send e-mail Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 04 Nov 2015, 15:43
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).
Post 04 Nov 2015, 15:43
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1631
Location: Toronto, Canada
AsmGuru62 04 Nov 2015, 18:00
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).
Post 04 Nov 2015, 18:00
View user's profile Send private message Send e-mail Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 04 Nov 2015, 19:33
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)
Post 04 Nov 2015, 19:33
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20344
Location: In your JS exploiting you and your system
revolution 04 Nov 2015, 22:24
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? Smile

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.
Post 04 Nov 2015, 22:24
View user's profile Send private message Visit poster's website Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 05 Nov 2015, 15:48
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.


Description:
Download
Filename: WrtNum5new.ASM
Filesize: 2.52 KB
Downloaded: 807 Time(s)

Post 05 Nov 2015, 15:48
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1631
Location: Toronto, Canada
AsmGuru62 05 Nov 2015, 18:40
The code from 1st post works fine.
I just tried in in debugger and the value is "+4789640" and not "+4789641".
Post 05 Nov 2015, 18:40
View user's profile Send private message Send e-mail Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 06 Nov 2015, 03:20
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? Sad
Post 06 Nov 2015, 03:20
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1631
Location: Toronto, Canada
AsmGuru62 06 Nov 2015, 11:44
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.

... added ...

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

... added ...

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".
Post 06 Nov 2015, 11:44
View user's profile Send private message Send e-mail Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 07 Nov 2015, 04:21
thank you very much AsmGuru62.it worked like a charm. Very Happy
Post 07 Nov 2015, 04:21
View user's profile Send private message Send e-mail Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 07 Nov 2015, 14:01
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 Very Happy

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.
Post 07 Nov 2015, 14:01
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 07 Nov 2015, 14:41
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
Post 07 Nov 2015, 14:41
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 07 Nov 2015, 15:02
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
Post 07 Nov 2015, 15:02
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 07 Nov 2015, 16:55
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. Smile
Post 07 Nov 2015, 16:55
View user's profile Send private message Send e-mail Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 07 Nov 2015, 17:10
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
Post 07 Nov 2015, 17:10
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 150
Location: Piliyandala,Sri lanka
mns 07 Nov 2015, 19:23
thanks HaHaAnonymous.
also less work ,less time consuming and less freedom.
Post 07 Nov 2015, 19:23
View user's profile Send private message Send e-mail Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 08 Nov 2015, 07:48
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.
Post 08 Nov 2015, 07:48
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.