flat assembler
Message board for the users of flat assembler.
Index
> Main > how to detect length of floating point value? |
Author |
|
Goplat 02 Jan 2007, 19:29
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.
|
|||
02 Jan 2007, 19:29 |
|
Vasilev Vjacheslav 03 Jan 2007, 11:29
so it's better to convert it to a string with rounding and get length, i am right?
|
|||
03 Jan 2007, 11:29 |
|
Borsuc 03 Jan 2007, 18:58
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 Hope it helps |
|||
03 Jan 2007, 18:58 |
|
mattst88 03 Jan 2007, 23:15
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 |
|||
03 Jan 2007, 23:15 |
|
f0dder 03 Jan 2007, 23:20
Why do you need to get the length? Perhaps there's other solutions?
|
|||
03 Jan 2007, 23:20 |
|
kohlrak 04 Jan 2007, 04:44
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. |
|||
04 Jan 2007, 04:44 |
|
Vasilev Vjacheslav 04 Jan 2007, 11:11
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(); |
|||
04 Jan 2007, 11:11 |
|
kohlrak 04 Jan 2007, 12:29
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...
|
|||
04 Jan 2007, 12:29 |
|
f0dder 04 Jan 2007, 21:33
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.
|
|||
04 Jan 2007, 21:33 |
|
cod3b453 04 Jan 2007, 21:56
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....
|
|||
04 Jan 2007, 21:56 |
|
mattst88 04 Jan 2007, 22:11
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 |
|||
04 Jan 2007, 22:11 |
|
Plue 05 Jan 2007, 16:51
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 |
|||
05 Jan 2007, 16:51 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.