Jump to content
  • Advertisement
Sign in to follow this  
KKTHXBYE

represent float as two ints then subtract it by another to get exact value not approximation

This topic is 609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i need to represent float as two values int.int

 

then if i have two such floats i need to subtract them but i don't know how.

 

The reason of doing such thing is that i need to precisely subtract one from another like 14.32 - 15.89 => -1.57

 

i am not sure if i could just use these two numbers as single floats then subtract them and  then just round up the result 14.32 - 15.89 =  -1.int(57+0.005)

but then theres what if -1 is 0 etc etc too much complications on fly i need exact numbers not approximations

Share this post


Link to post
Share on other sites
Advertisement
What you’re looking for is fixed point or Q. How wide are your integers there? If possible, try to keep it within a primitive int type, such as a int64_t. Q32.32 or even Q48.16 should be enough, depending on your use case. Otherwise, you may need a datatype that expresses a float in terms of A + B/scale. Be careful because fixed point arithmetic have its own set of problems. It’s much easier to overflow or underflow in fixed point world than it is with typical IEEE 754 floats.

If you need something more precise, there are libraries that already do so: List of Arbitrary-Precision Arithmetic Software. Edited by fastcall22

Share this post


Link to post
Share on other sites

i need something liek this

 

dodawanie-odejmowanie-u%C5%82amk%C3%B3w-

 

i dont even want to implement this but i think ill have too

 

i need to subtract personel days off since for 2 months there is 1.67 dayoff i need to use fkn floats for that 

Edited by WiredCat

Share this post


Link to post
Share on other sites
Have you looked into fix point?

You shouldn't have to worry about this detail unless you require both high accuracy and a very wide range of values. This is more typical of scientific or engineering applications, not games.

Share this post


Link to post
Share on other sites

thats 

 

Have you looked into fix point?

You shouldn't have to worry about this detail unless you require both high accuracy and a very wide range of values. This is more typical of scientific or engineering applications, not games.

thats not a game, its a commercial app - i need to get real values not approximations Dont want to be charged for anything :P

 

mhm BCD im too dumb for that

 

 

funny thing is that i dont know how to subtract these nums like in this picture i gave in second post x_X maturity lel

Edited by WiredCat

Share this post


Link to post
Share on other sites

Is there a limit to the range of numbers involved?  Is there a limit to how many decimal places you need?

 

edit - also what language.

Edited by Infinisearch

Share this post


Link to post
Share on other sites
Fixed point does not approximate. Again, have you actually looked into it? In addition, fastcall22 linked you to arbitrary precision libraries, have you looked at those?

Finally, think about the business process you are replacing. Does it feature a calculator, software or spreadsheet somewhere? Perhaps the business is already used to dealing with the limitations imposed by numeric constraints.

There will always be a limitation, you can invest engineering effort into reducing the number of cases where it will arise but you cannot make the limitations disappear - after all you're working with real computers with finite CPU resources, memory and storage.

Share this post


Link to post
Share on other sites

i took the path to use that numeric approach like on picture. Tommorow ill ask few women to teach me that again. no worries ill make it and post code here

Share this post


Link to post
Share on other sites

Fixed-point math is just the binary equivalent of fixed-width math. Imagine you have a spreadsheet where each cell can contain only one digit, and there are eight columns. If you want each row to represent one number, you can choose any column you want to represent the "ones" digit; all those to the left are larger value (tens, hundreds, etc), and those to the right are smaller value (tenths, hundredths, etc). The entire row is called the "mantissa" or "significand", and you choose a fixed point in the representation (hence the name) to be the decimal point in the number it represents.

 

Ignoring signs and overflows, basic arithmetic is nearly identical on fixed-point math of arbitrary "ones" alignment as it is on fixed-point math where there are no fractional digits (e.g. all fixed-width numeric representations, such as 32-bit integers). Addition/subtraction; just sum or subtract the significands. Multiplication; multiply the significands, then shift the significand down to align the result's (virtual) decimal point with the original values' (virtual) decimal point. Work it out in long-form arithmetic on paper, and you should start to see the resemblance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!