Skip to content

"Parse" float and double to a structure, and given a specific value, you can get the nearest bigger and smaller value.

Notifications You must be signed in to change notification settings

VigourJiang/StructuredFloat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StructuredFloat

"Parse" float and double to a structure, and given a specific value, you can get the nearest bigger and smaller value.

This project is Written in java and maven, and it has no compile scope dependencies on other jars.

-------------------Sample Usage--------------------------------------

double cur = 3.23d; // any valid double value 
StructuredFloat sd = com.vigour.StructuredFloat.StructedFloatBuilder.buildDouble(cur);
StructuredFloat smaller = sd.absSmaller();
StructuredFloat bigger = sd.absBigger();

if(smaller != null){
  double smaller_double = smaller.getBigDecimal().doubleValue();
  // now you get the nearest double value whose absolute value is smaller
}
if(bigger!= null){
  double bigger_double = bigger.getBigDecimal().doubleValue();
  // now you get the nearest double value whose absolute value is bigger
}

Note that, Double.MAX_VALUE has no double value with bigger absolute value, and zero has no double value with smaller absolute value. That's the reason you have to check "StructuredFloat.absSmaller() != null" and "StructuredFloat.absBigger() != null" The same usage style also applies to float.

-------------Some Interesting Resule for Double--------------------

Nearest smaller double: 0.09999999999999999167332731531132594682276248931884765625 Current double: 0.1000000000000000055511151231257827021181583404541015625 Nearest bigger double : 0.10000000000000001942890293094023945741355419158935546875

Nearest smaller double: 0.99999999999999988897769753748434595763683319091796875 Current double: 1 Nearest bigger double : 1.0000000000000002220446049250313080847263336181640625

Nearest smaller double: 9.9999999999999982236431605997495353221893310546875000 Current double: 10.00 Nearest bigger double : 10.0000000000000017763568394002504646778106689453125000

Nearest smaller double: 99999999999999.9843750000000000000000000000000000000000000000000000 Current double: 100000000000000.00000000000000000000000000000000 Nearest bigger double : 100000000000000.0156250000000000000000000000000000000000000000000000

Nearest smaller double: 999999999999999.8750000000000000000000000000000000000000000000000000 Current double: 1000000000000000.0000000000000000000000000000000000 Nearest bigger double : 1000000000000000.1250000000000000000000000000000000000000000000000000

Nearest smaller double: 9999999999999998.0000000000000000000000000000000000000000000000000000 Current double: 10000000000000000.0000000000000000000000000000000000000 Nearest bigger double : 10000000000000002.0000000000000000000000000000000000000000000000000000

Nearest smaller double: 99999999999999984.0000000000000000000000000000000000000000000000000000 Current double: 100000000000000000.000000000000000000000000000000000000000 Nearest bigger double : 100000000000000016.0000000000000000000000000000000000000000000000000000

Nearest smaller double: 9999999999999998174371273630364736815867488735718786093662414371947263704524926751224722911637244940234972882804879769415602664816552507597839565690480126952738889402600333599657997758603312171995012866291845554976690497648524473448849371595248581587050582985041870802940253992811266476846330599148879872.0000000000000000000000000000000000000000000000000000 Current double: 9999999999999999392535525055364621860040287220117324953190771571323204563013233902843309257440507748436856118056162172578717193742636030530235798840866882774987301441682011041067710253162440905843719802548551599076639682550821832659549112269607949805346034918662572406407604380845959862074904348138143744.000000000000000000000000000000000000000000000000 Nearest bigger double : 10000000000000000610699776480364506904213085704515863812719128770699145421501541054461895603243770556638739353307444575741831722668719553462632031991253638597235713480763688482477422747721569639692426738805257643176588867453119191870248852943967318023641486852283274009874954768880653247303478097127407616.0000000000000000000000000000000000000000000000000000

-------------Some Interesting Resule for Float--------------------

Nearest smaller float: null Current float: 0.0 Nearest bigger float: 1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125E-45

Nearest smaller float: 0.0999999940395355224609375 Current float: 0.100000001490116119384765625 Nearest bigger float: 0.10000000894069671630859375

Nearest smaller float: 0.999999940395355224609375 Current float: 1 Nearest bigger float: 1.00000011920928955078125

Nearest smaller float: 9.99999904632568359375000 Current float: 10.00 Nearest bigger float: 10.00000095367431640625000

Nearest smaller float: 99999.99218750000000000000000 Current float: 100000.00000000000 Nearest bigger float: 100000.00781250000000000000000

Nearest smaller float: 999999.93750000000000000000000 Current float: 1000000.0000000000000 Nearest bigger float: 1000000.06250000000000000000000

Nearest smaller float: 9999999.00000000000000000000000 Current float: 10000000.0000000000000000 Nearest bigger float: 10000001.00000000000000000000000

Nearest smaller float: 99999992.00000000000000000000000 Current float: 100000000.000000000000000000 Nearest bigger float: 100000008.00000000000000000000000

Nearest smaller float: 99999986661652122824821048795547566080.00000000000000000000 Current float: 99999996802856924650656260769173209088.00000000000000000000000 Nearest bigger float: 100000006944061726476491472742798852096.0000000000000000000000

About

"Parse" float and double to a structure, and given a specific value, you can get the nearest bigger and smaller value.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages