flat assembler
Message board for the users of flat assembler.

Index > Main > how to detect length of floating point value?

Author
Thread Post new topic Reply to topic
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
i have little stupid question, it is possible to detect length of floating point value without converting it to a string? and how to do it? thanks for help

for example, fp: 123.45678000000, it's length is 8 + dot

_________________
[not enough memory]
Post 02 Jan 2007, 09:41
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
The "length" of a floating point number is not very meaningful. There really is no 123.45678 in floating point. The closest number you can get in double-precision is 123.4567799999999948568074614740908145904541015625, so its length is really 49.
Post 02 Jan 2007, 19:29
View user's profile Send private message Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
so it's better to convert it to a string with rounding and get length, i am right?
Post 03 Jan 2007, 11:29
View user's profile Send private message Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
Floating point format is "binary".. don't think in decimal!! It's better if you think this way:

For example, 3.5 is expressed as: 11.10000000000000...
This means "three" and half.

That is, it's not easy to get the length of it by doing what you said -- and floating point is EVEN more tricky.. imagine this number:

1101001010100010

Okay, now where is the point? Actually, floating point "moves" the point. For example, it could be in the middle:

11010010.10100010

Or it could be on the left, symbolizing a smaller number (that is, you get "better" precision on small numbers, as more bits are used for the mantissa (the part that composes the fractional part)).

Here, to the left:

110.1001010100010

These are two different numbers, because the "point" is on different locations. That's what floating point is about really Smile

Hope it helps
Post 03 Jan 2007, 18:58
View user's profile Send private message Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
Vasilev Vjacheslav wrote:
so it's better to convert it to a string with rounding and get length, i am right?


Yes, I think so, because there is no easy correlation with the actual binary value and the length of the string.

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
Post 03 Jan 2007, 23:15
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Why do you need to get the length? Perhaps there's other solutions?
Post 03 Jan 2007, 23:20
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
I really havn't gotten much into FPU in assembly, but my C++ book (which seems to be wrong about everything else, but since this is a nice theory on how impliment FPU into code, i'll consider this section to actually be right as in a good algoritham, but i doubt it's true for assembly) states that FP numbers have 2 parts. An actual number, then the part following it being scientific notation. For instance we could make a float of 4 bytes. 3 bytes could be the number, and 1 byte could be used for the scientific notation. The rest of this reply will assume that you understand this paragraph.

42.22 would be displayed as: 0x00107E01 (0x00107E [4222] * 10^0x01)

You could use that and some math to find the length.
Post 04 Jan 2007, 04:44
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
thanks all for help

The_Grey_Beast, yes, i trying to thing in decimal, that's my problem

f0dder, my program getting some text from edit control doing conversion to fpu and than some math manipulation, after all i must check it for length, i using this way for now (in pseudocode):

Code:
GetDlgItemText();
x=_atof();
x=x ^ 100;
if(x > 999.99) _error();
    
Post 04 Jan 2007, 11:11
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak
It's safe to assume that you're not going to xor (i'm assuming that ^ is what you're using) won't work well with a float, so you'll be stuck with whole numbers so there's no need for floats. On the other hand, you could go ahead and just mul your variable by 2 and take away the decimal in your if. By the way, it's usually not good to have a float in your ifs...
Post 04 Jan 2007, 12:29
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
floats in if statements is fine, just never compare them for equality with ==, you need to test if difference is larger than very_small_amount.
Post 04 Jan 2007, 21:33
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
the length of the integer part [before the .] can be calculated by doing log10() then ceiling function - you could compare this answer to 4, if it was equal or above then x > 999.999....
Post 04 Jan 2007, 21:56
View user's profile Send private message Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
kohlrak wrote:
It's safe to assume that you're not going to xor (i'm assuming that ^ is what you're using) won't work well with a float, so you'll be stuck with whole numbers so there's no need for floats. On the other hand, you could go ahead and just mul your variable by 2 and take away the decimal in your if. By the way, it's usually not good to have a float in your ifs...


No, I believe he's using ^ for raising to a power.

_________________
My x86 Instruction Reference -- includes SSE, SSE2, SSE3, SSSE3, SSE4 instructions.
Assembly Programmer's Journal
Post 04 Jan 2007, 22:11
View user's profile Send private message Visit poster's website Reply with quote
Plue



Joined: 15 Dec 2005
Posts: 151
Plue
First, get the integer part of the number. Calculate its length. Then get the fractional part of your float as an integer and calculate its length. Add the two to get the total length.
This will give you an approximation of the fractional part of the float as an integer:
Code:
Float.f = 1.1926
Float = (Float-Int(Float))
Double.d = Float
Int = Double * 10000000
For I = 7 To 1 Step -1
  MF = Pow(10, I)
  If Int % MF = 0
    Int / MF
    Break
  EndIf
Next    
Post 05 Jan 2007, 16:51
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.