Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Passing values from structure arrays to a generic geometrical mean function Date: Tue, 31 Jan 2017 13:35:54 -0800 Organization: None to speak of Lines: 67 Message-ID: References: <0a48658d-968a-4ddf-8c3e-5a856e298ec1@googlegroups.com> <87lgtwxxjp.fsf@bsb.me.uk> <50df88d5-9db9-401b-bf2d-bf1846d68b7b@googlegroups.com> <87wpdgvzmp.fsf@bsb.me.uk> <522dd411-1a86-49fb-8df7-a1384f3c5da8@googlegroups.com> <4f8b2e49-6a8b-fb05-e35b-d21fb00f6779@verizon.net> <899963c8-4716-422b-93c9-59e98c91a160@googlegroups.com> <43aa17b1-6286-8ec5-c9d7-fe59d88c1688@verizon.net> <39f310e9-691c-4a27-9d9f-bc9114854af8@googlegroups.com> <816ac4fc-3e93-cd1b-e4b9-ce0eac72da7e@verizon.net> <7af44069-dfe1-48ce-8c6e-33aed7131a02@googlegroups.com> <54ce71f7-5db5-0d83-c2f6-bc37acf070c1@verizon.net> <2be1c4a8-64ed-4e33-86fc-00ae8dee873b@googlegroups.com> <87sho1plyn.fsf@bsb.me.uk> <8d0708ec-7ccf-e349-b973-f53869988664@verizon.net> <496c32c2-a962-44f3-94eb-6ac84b3ed9b6@googlegroups.com> <053298c7-0b3f-45c6-a1b2-a53663fca412@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="cca5ec70fa32bd72c3a8e20925c507dd"; logging-data="6532"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VRkGqy/+drXlJpDGfyRnG" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:lyCkbrNCprzttfMamqyia1O3RX4= sha1:FojjqIbH5Ug7///33DRtLEkj060= Xref: csiph.com comp.lang.c:102875 Alla _ writes: > On Monday, January 30, 2017 at 11:18:19 PM UTC+3, Keith Thompson wrote: >> Alla _ writes: [...] >> > double mean_twonums(double number1, double number2) >> > { >> > // if both abs values of numbers are between 1 and 0 >> > if( (fabs(number1) < 1 && fabs(number1) > 0) >> > && (fabs(number2) < 1 && fabs(number2) > 0) ) >> > { >> > number1 += 1.0; >> > number2 += 1.0; >> > >> > return pow((number1*number2), 1.0/2) - 1; >> > } >> > // if any number is negative return simple arithm average >> > if(number1 < 0 || number2 < 0) >> > return (number1+number2)/2; >> > //compute geo mean for any other cases >> > return pow((number1*number2), 1.0/2); >> > } >> >> I can't comment on whether this function is meaningful in financial >> terms, but I find the name "mean_twonums" misleading at best. >> The name strongly implies that it computes the mean of two numbers, >> which should be as simple as >> return (number1 + number2) / 2.0; >> for an arithmetic mean, or >> return sqrt(number1 * number2); >> for a geometric mean. There are other kinds of means, such as the >> harmonic mean, but the value computed by this function doesn't resemble >> any of them. >> > Yes, this is exactly what it does. Indeed it has been mentioned many > times that i have to change the pow to sqrt. Truly sorry for not doing > it yet. Using sqrt() rather than pow() is a minor side issue, affecting clarity and perhaps performance. *What* is exactly what it does? My point is this: I can understand what the function actually does, because I understand C well enough to follow it. What I cannot (yet) understand is *why* it does what it does. Do you agree that name "mean_twonums" is misleading? I'm asking (as others have asked) what is the actual purpose of this function? Is there some actual reason for computing either an arithmetic mean, or a geometric mean, or sqrt((x+1)*(y+1))-1, depending on the values of the arguments? Does this function have some name in the context of financial calculations? If you don't want to answer that question, by all means say so and I'll drop it -- but I can't figure out why you don't want to answer it. You're asking for help with your program. Understanding the meaning of this function would help us do that. Without that, we can't tell whether the function is correct or not. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"