I'm trying to detect whether I've multiplied the numerator and denominator of a complex fraction enough times (by 10/10) to get integers on both top and bottom. Often my algorithm works. Sometimes, though, the Int() function doesn't work as expected and I get huge numbers out of it. Here is a simplified version of what I'm running:
<cfoutput>
<cfset RealNumber = RandRange(1000, 10000) / 1000 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#Int(RealNumber)#"<br />
<cfloop condition="RealNumber NEQ Int(RealNumber)">
<cfset RealNumber = RealNumber * 10 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#Int(RealNumber)#"<br />
</cfloop>
</cfoutput>
Most of the time this gets what I want, but sometimes it looks like this:
RealNumber = "9.184" Int(RealNumber) = "9"
RealNumber = "91.84" Int(RealNumber) = "91"
RealNumber = "918.4" Int(RealNumber) = "918"
RealNumber = "9184" Int(RealNumber) = "9183"
RealNumber = "91840" Int(RealNumber) = "91839"
RealNumber = "918400" Int(RealNumber) = "918399"
RealNumber = "9184000" Int(RealNumber) = "9183999"
RealNumber = "91840000" Int(RealNumber) = "91839999"
RealNumber = "918400000" Int(RealNumber) = "918399999"
RealNumber = "9184000000" Int(RealNumber) = "9183999999"
RealNumber = "91840000000" Int(RealNumber) = "91839999999"
RealNumber = "918400000000" Int(RealNumber) = "918399999999"
RealNumber = "9.184E+012" Int(RealNumber) = "9.184E+012"
RealNumber = "9.184E+013" Int(RealNumber) = "9.184E+013"
RealNumber = "9.184E+014" Int(RealNumber) = "9.184E+014"
RealNumber = "9.184E+015" Int(RealNumber) = "9.184E+015"
Or even like this:
RealNumber = "2.152" Int(RealNumber) = "2"
RealNumber = "21.52" Int(RealNumber) = "21"
RealNumber = "215.2" Int(RealNumber) = "215"
RealNumber = "2152" Int(RealNumber) = "2152"
RealNumber = "21520" Int(RealNumber) = "21520"
RealNumber = "215200" Int(RealNumber) = "215200"
RealNumber = "2152000" Int(RealNumber) = "2152000"
RealNumber = "21520000" Int(RealNumber) = "21520000"
RealNumber = "215200000" Int(RealNumber) = "215200000"
RealNumber = "2152000000" Int(RealNumber) = "2152000000"
RealNumber = "21520000000" Int(RealNumber) = "21520000000"
RealNumber = "215200000000" Int(RealNumber) = "215200000000"
RealNumber = "2.152E+012" Int(RealNumber) = "2.152E+012"
RealNumber = "2.152E+013" Int(RealNumber) = "2.152E+013"
RealNumber = "2.152E+014" Int(RealNumber) = "2.152E+014"
RealNumber = "2.152E+015" Int(RealNumber) = "2.152E+015"
RealNumber = "2.152E+016" Int(RealNumber) = "2.152E+016"
What am I doing wrong and how do I compensate for this?
RealNumber = "9.184E+015"
The output is deceptive. If you use #RealNumber.toString()# the real value is probably something like 9.183999999999998E15. CF uses the approximate type Double for most mathematical operations. So what you are seeing is normal behavior for floating point numbers. If you need greater accuracy, use PrecisionEvaluate. It makes use of BigDecimals for arithmetic operations (because they are more precise than java.lang.Double).
Try using javacast.. ColdFusion is trying to dynamically determine your variable type and is not doing a great job.. Javacast covers a multitude of sins.
<cfoutput>
<cfset RealNumber = RandRange(1000, 10000) / 1000 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#Int(RealNumber)#"<br />
<cfloop condition="RealNumber NEQ Int(RealNumber)">
<cfset RealNumber = RealNumber * 10 />
RealNumber = "#RealNumber#" Int(RealNumber) = "#javaCast("int", RealNumber)#"<br />
</cfloop>
Val() seems to correct it too:
<cfoutput>
<cfset RealNumber = RandRange(1000, 10000) / 1000 />
"#val(RealNumber)#" = INT: "#val(Int(RealNumber))#"<br />
<cfloop condition="val(RealNumber) NEQ Val(Int(RealNumber))">
<cfset RealNumber = RealNumber * 10 />
"#val(RealNumber)#" INT: "#val(Int(RealNumber))#"<br />
</cfloop>
</cfoutput>
Related
I want to extract some keys and their values from a string.
It seems that the program needs using regex, but I do not know it well
It seems that the program needs using regex, but I do not know it well
The string is like this
adprod_type^B6^Apr_flag^B0^Asource^B^Asource_id^B^Aadprod_params^Bheight^D^Chtml^D<style>img{width:100% !important; height:100% !important}</style>\\n<script>gAdController.passback();</script>^Cwidth^D^Aadprodset_id^B9123^Aadserve^B
What expected is like this
adprod_type:'6',pr_flag:'0',source:'',source_id:'',adprod_params:{height:'',html:'<style>img{width:100% !important; height:100% !important}</style>\\n<script>gAdController.passback();</script>',width:''},adprodset_id:'9123',adserve:''
I tried this :
file_obj = open("/Users/icko/Documents/000/a.txt")
all_lines = file_obj.readlines()
for line in all_lines:
a = line.split('^A')
for i in a:
b = i.split('^B')
key = b[0]
val = b[1]
data[key] = val
if data['adprod_type'] == '6':
print('key=' + key)
print('value=' + val)
file_obj.close()
Thank you
I tried this:
if __name__ == '__main__':
file_obj = open("/Users/icko/Documents/000/a.txt")
all_lines = file_obj.readlines()
for line in all_lines:
data = {}
a = line.split('')
for i in a:
b = i.split('')
key = b[0]
val = b[1]
data[key] = val
if data['adprod_type'] == '6':
metadata = {}
metadata1 = {}
metadata2 = {}
metadata3 = {}
subdata = data['adprod_params']
c = subdata.split('')
data1 = c[0].split('')
data2 = c[1].split('')
data3 = c[2].split('')
key1 = data1[0]
key2 = data2[0]
key3 = data3[0]
val1 = data1[1]
val2 = data2[1]
val3 = data3[1]
metadata1[key1] = val1
metadata2[key2] = val2
metadata3[key3] = val3
metadata = dict(metadata1, **metadata2, **metadata3)
print(metadata)
success!
I need to edit some URL data, but i am facing some issue, the url is coming as:
<cfset myurl = "http:/example.com/0.asp?rpttype=298&companyQ=148&companyQ=150&companyQ=176&companyQ=186&companyQ=195&companyQ=105&companyQ=136&companyQ=126&productQ=1072&productQ=1042&productQ=1043stateQ=&sortBy=1&sortOrder=1">
<cfset reURL = queryStringDeleteVar("companyQ",myurl)>
<cfset reURL = queryStringDeleteVar("productQ",reURL)>
<cfset reURL = reURL & "&companyQ=">
<cfset listData = ''>
<cfloop list="#getCompanyID#" index="k">
<cfset listData = ListChangeDelims(ListPrepend(listData,"%27+or+q2.comp+%3D%27=" & k),'',',')>
</cfloop>
<cfdump var="#reURL##listData#" label="URL Rewritten">
rewriting as:
http://example.com/0.asp?rpttype=298&stateQ=&sortBy=1&sortOrder=1&companyQ=%27+or+q2.comp+%3D%27=186%27+or+q2.comp+%3D%27=176%27+or+q2.comp+%3D%27=150%27+or+q2.comp+%3D%27=148
but it needs to be like this
http://example.com/0.asp?rpttype=298&stateQ=&sortBy=1&sortOrder=1&companyQ=186%27+or+q2.comp+%3D%27=176%27+or+q2.comp+%3D%27=150%27+or+q2.comp+%3D%27=148
i am missing something,
please guide
this
companyQ=%27+or+q2.comp+%3D%27=186
needs to like this for the first one only
companyQ=186
only the first one, remaining will stay as it is:
the value 186 is dynamic
I think this should just help you.
<cfset myurl = "http:/example.com/0.asp?rpttype=298&companyQ=148&companyQ=150&companyQ=176&companyQ=186&companyQ=195&companyQ=105&companyQ=136&companyQ=126&productQ=1072&productQ=1042&productQ=1043stateQ=&sortBy=1&sortOrder=1">
<cfset reURL = queryStringDeleteVar("companyQ",myurl)>
<cfset reURL = queryStringDeleteVar("productQ",reURL)>
<cfset reURL = reURL & "&companyQ=#listFirst(getCompanyID)#">
<cfset getCompanyID = listDeleteAt(getCompanyID,1)>
<cfset listData = ''>
<cfloop list="#getCompanyID#" index="k">
<cfset listData =ListPrepend(listData,"%27+or+q2.comp+%3D%27=" & k)>
</cfloop>
<cfset listData = listChangeDelims(listData,"") />
<cfdump var="#reURL##listData#" label="URL Rewritten">
I am trying to replicate the IRR (internal rate of return) function in excel. I found one cfc in riaforge.com but it doesn't return the same value as the excel's irr.
The newton - raphson method uses derivatives and I am not sure how to calculate derivatives in coldfusion.
year cash flow
---- --------
0 -4000
1 1200
2 1410
3 1875
4 1050
should return 14.3% ( from wikipedia's example )
Has anybody done this before?
thanks
Extending to what Jason said, you would need to implement a code that works efficiently and not rely on the brute force algorithm that Falconeyes suggested. nothing personal here the first time i programmed IRR as a server side script it was using brute force and a day later my web host called me as said they were taking my site offline as the code was consuming 100% system resources
What follows is a step by step IRR calculation using Newton Raphson method and you can follow it and implement the ideas in Cold Fusion
f(x) = -4000(1+i)^0 +1200(1+i)^-1 +1410(1+i)^-2 +1875(1+i)^-3 +1050(1+i)^-4
f'(x) = -1200(1+i)^-2 -2820(1+i)^-3 -5625(1+i)^-4 -4200(1+i)^-5
x0 = 0.1
f(x0) = 382.0777
f'(x0) = -9560.2616
x1 = 0.1 - 382.0777/-9560.2616 = 0.139965195884
Error Bound = 0.139965195884 - 0.1 = 0.039965 > 0.000001
x1 = 0.139965195884
f(x1) = 25.1269
f'(x1) = -8339.5497
x2 = 0.139965195884 - 25.1269/-8339.5497 = 0.142978177747
Error Bound = 0.142978177747 - 0.139965195884 = 0.003013 > 0.000001
x2 = 0.142978177747
f(x2) = 0.126
f'(x2) = -8256.0861
x3 = 0.142978177747 - 0.126/-8256.0861 = 0.142993440675
Error Bound = 0.142993440675 - 0.142978177747 = 1.5E-5 > 0.000001
x3 = 0.142993440675
f(x3) = 0
f'(x3) = -8255.6661
x4 = 0.142993440675 - 0/-8255.6661 = 0.142993441061
Error Bound = 0.142993441061 - 0.142993440675 = 0 < 0.000001
IRR = x4 = 0.142993441061 or 14.3%
I don't know what ColdFusion is, but the idea for finding IRR is very simple.
The IRR is a number r such that
sum i = 0 to N C_i * (1 + r)^(-t_i) = 0
where there are N + 1 cashflows C_0, C_1, ..., C_N at times t_0, t_1, ..., t_N. Define
f(r) = sum i = 0 to N C_i * (1 + r)^(-t_i).
Then
f'(r) = sum i = 0 to N -C_i * (1 + r)^(-t_i - 1).
Choosing an initial guess r_0 and iterate via
r_{n + 1} = r_n - f(r_n) / f'(r_n)
In your specific example, you have
t_0 = 0 C_0 = -4000
t_1 = 1 C_1 = 1200
t_2 = 2 C_2 = 1410
t_3 = 3 C_3 = 1875
t_4 = 4 C_4 = 1050
Try a guess of r_0 = 0.1.
Again, I don't know what ColdFusion is, but it has to be a programming language, and so it should allow this basic math to be computed.
<cffunction name="calcIRR">
<cfargument name="arrCashFlow" type="Array" required="true" hint="array of cashflow">
<cfscript>
var guess = 0.1;
var inc = 0.00001;
do {
guess += inc;
npv = 0; //net present value
for (var i=1; i<=arrayLen(arguments.arrCashFlow); i++) {
npv += arguments.arrCashFlow[i] / ((1 + guess) ^ i);
}
} while ( npv > 0 );
guess = guess * 100;
</cfscript>
<cfreturn guess>
</cffunction>
<cfscript>
cFlow = arrayNew(1);
cFlow[1] = -4000;
cFlow[2] = 1200;
cFlow[3] = 1410;
cFlow[4] = 1875;
cFlow[5] = 1050;
c = calcIRR(cFlow);
</cfscript>
<cfdump var="#cFlow#">
<cfdump var="#c#">
I tried all submited solutions and none of them worked like it should(like in excel).
Here is the code for calculating XIRR that is working like it should. For the IRR you just need to change this line:
<cfset npv = npv + (arguments.values[i] / ((1 + arguments.rate) ^ time_span))>
And here is the whole script
<cfoutput>
#XIRR(values=[-5000,500,110500], dates=["2015-07-06","2016-07-06","2017-07-06"])#
</cfoutput>
<cffunction name="XIRR">
<cfargument name="values" required="true">
<cfargument name="dates" required="true">
<cfset var do_calculation = check_data(values=arguments.values, dates=arguments.dates)>
<cfif do_calculation.is_ok>
<cfset var rate = 1>
<cfset var npv = calculate_NPV(rate=rate, values=arguments.values, dates=arguments.dates)>
<cfloop condition="#npv# gt 10e-6">
<cfset rate = rate + 1>
<cfset npv = calculate_NPV(rate=rate, values=arguments.values, dates=arguments.dates)>
</cfloop>
<cfloop from="1" to="6" index="pow">
<cfset fac = 1 / (10 ^ pow)>
<cfset npv = 0>
<cfloop condition="#npv# lt 10e-6">
<cfset rate = rate - fac>
<cfset npv = calculate_NPV(rate=rate, values=arguments.values, dates=arguments.dates)>
</cfloop>
<cfset rate = rate + fac>
</cfloop>
<cfreturn rate>
<cfelse>
<cfreturn "error: #do_calculation.error#">
</cfif>
</cffunction>
<cffunction name="check_data">
<cfargument name="values" required="true">
<cfargument name="dates" required="true">
<cfset var is_ok = true>
<cfset var has_negative = false>
<cfset var has_positive = false>
<cfset var error = 0>
<cfset var return = structNew()>
<cfset var date_prev = "">
<cfset var date_curr = "">
<cfif arguments.values[1] gte 0>
<cfset is_ok = false>
<cfset error = -1>
</cfif>
<cfloop array="#arguments.values#" item="value">
<cfif value gt 0>
<cfset has_positive = true>
</cfif>
<cfif value lt 0>
<cfset has_negative = true>
</cfif>
</cfloop>
<cfif !has_negative or !has_positive>
<cfset is_ok = false>
<cfset error = -2>
</cfif>
<cfif arrayLen(arguments.values) neq arrayLen(arguments.dates)>
<cfset is_ok = false>
<cfset error = -3>
</cfif>
<cfloop from="2" to="#arrayLen(arguments.dates)#" index="d">
<cfset date_prev = arguments.dates[d-1]>
<cfset date_curr = arguments.dates[d]>
<cfif dateDiff("d", date_prev, date_curr) lte 0>
<cfset is_ok = false>
<cfset error = -4>
</cfif>
</cfloop>
<cfset return.is_ok = is_ok>
<cfset return.error = error>
<cfreturn return>
</cffunction>
<cffunction name="calculate_NPV">
<cfargument name="rate" required="false" default="1">
<cfargument name="values" required="true">
<cfargument name="dates" required="true">
<cfset var npv = arguments.values[1]>
<cfset var time_span = "">
<cfloop from="2" to="#arrayLen(arguments.values)#" index="i">
<cfset time_span = dateDiff('d', arguments.dates[1], arguments.dates[i]) / 365>
<cfset npv = npv + (arguments.values[i] / ((1 + arguments.rate) ^ time_span))>
</cfloop>
<cfreturn npv>
</cffunction>
I am working on migrating a bunch of data from an old database into a new one. In the process of migrating a created a UDF for my script that would give me a bunch of data that I need. When I run a loop that calls the UDF multiple times I find that the first iteration runs fine, but then the UDF disappears from the following iterations.
My code is:
<cffunction name="getCats" access="public">
<cfargument name="assignments" type="any" />
<cfquery name="getCats" datasource="pgdold">
SELECT c1.id AS id1, c1.category AS name1, c2.id AS id2, c2.category AS name2, c3.id AS id3, c3.category AS name3, c4.id AS id4, c4.category AS name4, c5.id AS id5, c5.category AS name5
FROM category c1
LEFT JOIN category AS c2 ON c2.parentid = c1.id
LEFT JOIN category AS c3 ON c3.parentid = c2.id
LEFT JOIN category AS c4 ON c4.parentid = c3.id
LEFT JOIN category AS c5 ON c5.parentid = c4.id
</cfquery>
<cfquery name="get" dbtype="query">
SELECT (name1 + '/' + name2 + '/' + name3 + '/' + name4 + '/' + name5) AS category
FROM getCats
WHERE
<cfloop query="Arguments.assignments">
(id1 = #Arguments.assignments.categoryid# OR id2 = #Arguments.assignments.categoryid# OR id3 = #Arguments.assignments.categoryid# OR id4 = #Arguments.assignments.categoryid# OR id5 = #Arguments.assignments.categoryid#)
<cfif Arguments.assignments.currentrow IS NOT Arguments.assignments.recordCount> OR </cfif>
</cfloop>
</cfquery>
<cfreturn get />
</cffunction>
<cfscript>
olddb = {datasource='pgdold'};
a = new Query(argumentCollection=olddb);
a.setSQL('SELECT * FROM products LIMIT 10');
p = a.execute();
pr = p.getResult();
</cfscript>
<cfscript>
products = arrayNew();
for(i=1;i<=pr.recordCount;i++){
product = {};
if(!reFind('([0-9]+\-)(G|g)(I|i)(F|f)(T|t)',pr['sku'][i])){
b = new Query(argumentCollection=olddb);
b.setSql('SELECT * FROM productpics WHERE sku = :sku ORDER BY picorder');
b.addParam(name='sku',value=pr['sku'][i]);
pics = b.execute().getResult();
picList = '';
for(j=1;j<=pics.recordCount;j++){
picList = picList & ';' & pics['imagename'][j];
}
d = new Query(argumentCollection=olddb);
d.setSql('SELECT * FROM skucategories WHERE sku = :sku');
d.addParam(name='sku', value=pr['sku'][i]);
assignments = d.execute().getResult();
categories = getCats(assignments);
writeDump(categories);
product = {
store = 'admin',
websites = 'base',
attribute_set = 'Default',
categories = '',
type = 'simple',
sku = pr['sku'][i],
name = reReplace(reReplace(pr['title'][i],'\"','&##34;','all'),'\,','&##44;','all'),
price = pr['price'][i],
description = reReplace(reReplace(pr['detail'][i],'\"','&##34;','all'),'\,','&##44;','all'),
short_description = '',
image = pics['imagename'][1],
small_image = pics['imagename'][1],
thumbnail = pics['imagename'][1],
weight = pr['weight'][i],
has_options = 1,
is_in_stock = 1,
qty = 1000,
disabled = 'No',
status = 'Enabled',
options_container = 'Black after info Column',
tax_class_id = 'Taxable Goods',
visibility = 'Catalog,Search',
gallery = right(picList,len(picList)-1) // Seperate images by semicolon (;)
};
arrayAppend(products,product);
}
}
</cfscript>
It is the getCats function that disappears.
* yes the code is ugly and inefficient. It wasn't meant to do anything more than this one job and after it finishes the job it is going to be thrown away, so don't tell me about the ugliness or inefficiencies.
Without reading every line of your code posted, I have a Strong feeling that it has to do with Var scooping... since you are calling that UDF in a Loop.
Please var scope you query variables.
Pre CF9:
<!--- insert after <cfargument> --->
<cfset var getCats = "">
<cfset var get = "">
At, or Post CF9 with Local scope:
<cfquery name="local.getCats" datasource="pgdold">
<cfquery name="local.get" dbtype="query">
You named a variable in the UDF with the same name as the UDF. Either var scope it or rename that query to something else. I recommend naming it something else.
I want to create equivalent CriteriaQuery to this query :
select u from User u where u.name = "John" and u.surname = "Black" and u.middlename = "Small";
but I have problem with "where" predicate.
Thanks
You can use:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery c = ...;
Root root = ...;
Predications name = builder.equal(root.get("name"), name);
Predications surname = ...;
Predications middlename = ...;
c.where(name, surname, middlename);