understanding precision and scale on a property - coldfusion

property name="poiLat" length="60" ormtype="big_decimal" persistent=true precision="16" scale="14" default="0" hint="";
I don't understand precision or scale correctly. Using the property above why would '1' give an error and '2' be accepted? what should I change it to to accept '1'
1 ) -118.27 = error
2) -18.27 = ok

Scale refers the number of digits to the right of the decimal place. If you have precision 16 and scale 14, you can only have 2 digits to the left of the decimal place, so
18.12345678901234 = ok
118.27 = error
Try:
precision="16" scale="13"
That will allow 118.1234567890123, but that is a lot of decimal places. How many do you really need?
precision="16" scale="4"
Will allow 123456789012.1234

Related

Python Function Returning Decimal Precision

I have a function which I want to return a decimal number 1.0000 [not a string], how can I do this without using NUMPY or SCIPY? No matter what I do, it returns it as 1.0.
Seems like you are looking for decimal package:
>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal('0.142857')
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')
The precision of Python's floats is about 16 digits, no matter how you write them. 1.0 is the same number as 1.0000. The difference is not in number, but in number-to-string conversion involved in display. Thus, the only way to do what you want is to, as you say, convert it to a string explicitly under your terms: "{:.4f}".format(1.0).

Python rounding error for ints in a list (list contains ints and strings)

I have a python (2.7) script that reads an input file that contains text setup like this:
steve 83 67 77
The script averages the numbers corresponding to each name and returns a list for each name, that contains the persons name along with the average, for example the return output looks like this:
steve 75
However, the actual average value for "steve" is "75.66666667". Because of this, I would like the return value to be 76, not 75 (aka I would like it to round up to the nearest whole integer). I'm not sure how to get this done... Here is my code:
filename = raw_input('Enter a filename: ')
file=open(filename,"r")
line = file.readline()
students=[]
while line != "":
splitedline=line.split(" ")
average=0
for i in range(len(splitedline)-1) :
average+=int(splitedline[i+1])
average=average/(len(splitedline)-1)
students.append([splitedline[0],average])
line = file.readline()
for v in students:
print " ".join(map(str, v))
file.close()
While your code is very messy and should be improved overall, the solution to your problem should be simple:
average=average/(len(splitedline)-1)
should be:
average /= float(len(splitedline) - 1)
average = int(round(average))
By default in Python 2.x / with two integers does flooring division. You must explicitly make one of the parameters a floating point number to get real division. Then you must round the result and turn it back into an integer.
In Python 3 flooring division is //, and regular division is /. You can get this behavior in Python 2 with from __future__ import division.

Format or Pattern for Decimal Numbers to display on axis on google charts

Good Morning,
When ever we create data table with certain data for Column Chart i.e.
**['Year', '% of ToTal Revenues ', '% of Total orders'],
['Feb12-July12', 0.25, 0.36],
['Aug12-Jan12', 0.58, 0.69],
['Feb13-July14', 0.47, 0.14],
['Aug13-Jan14', 0.62, 0.84]**
in the out put especially on VAxis the graph was displaying 0.1 to 0.98..
but when i a want to append % symbol to the given input values like 0.01%,0.02%,to 0.98% it was converting decimal into natural numbers that for ex 0.65 into 65 so what type of pattern i have to pass forex VAxis:{format:'#.##%'}};
Please Help Me
Thanks in advance
If I undestood right, you want to keep it as decimals instead of 0.5 = 50%. This should do the trick:
vAxis:{format: '#.#\'%\'' }

How to count rating?

My question is more mathematical. there is a post in the site. User can like and dislike it. And below the post is written for example -5 dislikes and +23 likes. On the base of these values I want to make a rating with range 0-10 or (-10-0 and 0-10). How to make it correctly?
This may not answer your question as you need a rating between [-10,10] but this blog post describes the best way to give scores to items where there are positive and negative ratings (in your case, likes and dislikes).
A simple method like
(Positive ratings) - (Negative ratings), or
(Positive ratings) / (Total ratings)
will not give optimal results.
Instead he uses a method called Binomial proportion confidence interval.
The relevant part of the blog post is copied below:
CORRECT SOLUTION: Score = Lower bound of Wilson score confidence interval for a Bernoulli parameter
Say what: We need to balance the proportion of positive ratings with the uncertainty of a small number of observations. Fortunately, the math for this was worked out in 1927 by Edwin B. Wilson. What we want to ask is: Given the ratings I have, there is a 95% chance that the "real" fraction of positive ratings is at least what? Wilson gives the answer. Considering only positive and negative ratings (i.e. not a 5-star scale), the lower bound on the proportion of positive ratings is given by:
(source: evanmiller.org)
(Use minus where it says plus/minus to calculate the lower bound.) Here p is the observed fraction of positive ratings, zα/2 is the (1-α/2) quantile of the standard normal distribution, and n is the total number of ratings.
Here it is, implemented in Ruby, again from the blog post.
require 'statistics2'
def ci_lower_bound(pos, n, confidence)
if n == 0
return 0
end
z = Statistics2.pnormaldist(1-(1-confidence)/2)
phat = 1.0*pos/n
(phat + z*z/(2*n) - z * Math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
end
This is extension to Shepherd's answer.
total_votes = num_likes + num_dislikes;
rating = round(10*num_likes/total_votes);
It depends on number of visitors to your app. Lets say if you expect about 100 users rate your app. When a first user click dislike, we will rate it as 0 based on above approach. But this is not logically right.. since our sample is very small to make it a zero. Same with only one positive - our app gets 10 rating.
A better thing would be to add a constant value to numerator and denominator. Lets say if our app has 100 visitors, its safe to assume that until we get 10 ups/downs, we should not go to extremes(neither 0 nor 10 rating). SO just add 5 to each likes and dislikes.
num_likes = num_likes + 5;
num_dislikes = num_dislikes + 5;
total_votes = num_likes + num_dislikes;
rating = round(10*(num_likes)/(total_votes));
It sounds like what you want is basically a percentage liked/disliked. I would do 0 to 10, rather than -10 to 10, because that could be confusing. So on a 0 to 10 scale, 0 would be "all dislikes" and 10 would be "all liked"
total_votes = num_likes + num_dislikes;
rating = round(10*num_likes/total_votes);
And that's basically it.

coldfusion - rounding to nearest 5 cents

In coldfusion, how does one round a decimal to the nearest 5 cents? So that a figure of 0.39675 would round up to 0.40. And if the figure was 0.3690, it would round down to 0.35.
I can't seem to find anything useful via google.
Sorry for the brief question, but I think that's all I can really input.
Multiply by 20, round it, divide by 20:
RoundedNumber = ( Round( Number * 20 ) / 20 )