Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am completly struck with this.

I was trying to search the internet, but gets nothing.
Is it even possible?
Looks like i need some real programming guru here...

Example:
i have 123456.65432 and i want to have a int value 65432 from it
using standart modf i will get 123456 and 0.65432

how to convert double 0.65432 into 65432 int in constant time?

There should be some CPU intrinstic or bits or math magic to do that.

P. s. please don't tell me how to do that in linear or log. time...

What I have tried:

I can use modf to get the fractional part without precision penalty, but what to do then?

The half-solution is to get a fractional part using modf and then multiple it on the value, based on the fractional part's size (100 for 0.32, 1000 for 0.456 etc), BUT still this involves precision losses:
for example, on my machine 999999999.99999905 * 10000000000000000000 woule lead to the last 5 changed to 4 and so on
Posted
Updated 23-Feb-16 14:30pm
v3
Comments
Patrice T 23-Feb-16 18:42pm    
Can you rephrase your question and give example of what you want.
Shvetsov Evgeniy 23-Feb-16 19:02pm    
I added an example, but the question itself is pretty straight and clear already..
Patrice T 23-Feb-16 19:13pm    
Way better.
Can you tell what you want to do with the value ?
Do you always want 5 figures ?
Shvetsov Evgeniy 23-Feb-16 19:33pm    
Nope, the digits count is undetermined
nv3 23-Feb-16 19:14pm    
Your question is not clear. What do you mean by: convert fraction part to int? Obviously you want to interpret the fractional part in decimal - as your example shows. As the fractional part is internally represented in binary format, the conversion to a decimal representation will deliver an infinitely long sequence of fractional digits (in many cases). Functions like printf generate a rounded representation with a certain number of digits. So, do you always want the int to be equal to the first N digits of the fractional parts?

The other question is what you mean by linear or log time? With regard to what? The number of digits? You only have one value to operate on.

This subject comes up frequently. There is no silver bullet. nv3 has pointed out the gotchas in the comments. You need to appreciate how doubles are stored. The binary representation of the real number value is usually just a close approximation of the number. When the number stored as a double is displayed (or the fractional part "converted" to an integer) it may not be quite what you expected. What has time got to do with it?

How to get the integer and fractional part of a decimal number as two integers in Arduino or C? - Stack Overflow[^]

c - Getting the fractional part of a double value in integer without losing precision - Stack Overflow[^]

floating point - Extract fractional part of double *efficiently* in C - Stack Overflow[^]
 
Share this answer
 
v5
Comments
Shvetsov Evgeniy 23-Feb-16 19:55pm    
Thx, but i already read those. The problem is that i need a fractional part NOT as a double (the standart 'modf' do this), i need it to be an integral number. Converting it to text and then back to number is too complicated. I looking for a much faster solution.
[no name] 23-Feb-16 20:05pm    
1. Read the last link carefully.
2. Why did you not put all that in your question?
3. Good luck with silver bullet.
The obvious answer is multiply by 100000 and you get an integer.
Since this is not rocket science, I guess you want something else but didn't explain what.

Since extracting the fractional part of a value has usually no meaning without the integer part, I guess it is part of something bigger.
There is heaps of methods to transform a fractional part to an integer, but if we don't know why, we can't tell what method to use.
Remember that we are not in your head and untold obvious things are obvious only to you, not us.

[edit]
there is no CPU instruction to do what you want because it simply have no meaning.
You can't have an exact conversion because you think with a fractional value in base 10 and the CPU know the value in base 2. This mean rounding in any case.
Quote:
for example, on my machine 999999999.99999905 * 10000000000000000000 would lead to the last 5 changed to 4 and so on
because of what I said above.
There is no solution to this exazct problem, the way you ask it.
 
Share this answer
 
v2
Comments
Shvetsov Evgeniy 23-Feb-16 20:39pm    
I want exactly what you talking about, but the next time original value would be 1.12 and the 100000 would't work. This is the first problem. The seond problem is that you can mangle the value that way (see 'What I have tried').

So i need a way to somehow transform a double value to integer value fastly and loosing nothing. I am assuming there should be some CPU inrinstic or somewhat similar to do that. If humnaity launches rockets i hope there way to do this too :))

'There is heaps of methods to transform a fractional part to an integer' - OK, what is the efficient then? DO you know how to do that involving the constant operations count?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900