Click here to Skip to main content
15,890,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.

I have a formula to calculate weights of some numbers in C#. I have 9 weights. Sum of these weights are 1. So,These are too small.
For instance after calculating these weights, the result is something like below:
1/17 , 1/17 ,1/17 ,1/17 ,1/17 ,1/17 ,1/17 ,1/17 ,9/17
When I want to store these weights in a parameter with double type, the stored values will be:
0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0
The problem is that I need the exact value of these weights to use in another formula, and they are too important in that formula. Sum of them should be 1.
What can I do for solving this problem? Please help me.


Thanks a lot.
Posted
Comments
Rob Philpott 22-Sep-14 4:50am    
Why would they be 0.0 each time??

You *could* find yourself in the situation where the sum of all the parts isn't exactly 1 due to imperfect precision, but the thing to do there is probably just adjust the last one by some stupidly small amount to deliver an exact 1.
k5_ce 22-Sep-14 8:46am    
Thanks for your answer. Regards

1 solution

The float or double types will easily hold values such as the above, see http://msdn.microsoft.com/en-us/library/678hzkk9.aspx[^] for the range that a double can hold.
 
Share this answer
 
Comments
k5_ce 22-Sep-14 8:47am    
Thanks for your answer. Regards

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