help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to print this function?


From: Max Brister
Subject: Re: How to print this function?
Date: Tue, 22 May 2012 14:25:18 -0600

On Tue, May 22, 2012 at 1:50 PM, Sergei Steshenko <address@hidden> wrote:
>
>
>
>
> ----- Original Message -----
>> From: marco atzeri <address@hidden>
>> To: address@hidden
>> Cc:
>> Sent: Tuesday, May 22, 2012 10:27 PM
>> Subject: Re: How to print this function?
>>
>> On 5/22/2012 8:26 PM, Sergei Steshenko wrote:
>>>
>>>
>>>  Though not related to your original question, 0.1 may be a periodic
>> fraction in your _binary_ CPU, so you better write
>>>
>>>
>>>  t = 0.1 * [-10:1:10];
>>>
>>
>> irrilevant in this case
>>
>> octave:4> [-1:0.1:1;]-0.1*[-10:1:10;] <eps
>> ans =
>>
>>   Columns 1 through 20:
>>
>>     1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
>> 1   1   1
>>
>>   Column 21:
>>
>>     1
>>
>> regards
>> Marco
>>
>
>
> And what ? You are going to perform relevance on case per case basis ?
>
> And you are absolutely sure you won't forget to check relevance if you switch 
> to 'long double' type ?
>
> But you didn't get my point in the first place, so I'll try to explain it in 
> more detail using "C".
>
>
> In "C" you can write
>
> /* variant 1 */
> double a[21];
>
> int i;
>
> unsigned step = 1;
>
> for(i = -10; i <= 10; i++)
>   {
>   a[i + 10] = 0.1 * (double)i;
>   }
>
>
> /* variant 2 */
> double a[21];
>
> double step = 0.1;
>
> double d;
>
> unsigned i = 0;
> for(d = -1.0; d <= 1; d += step)
>   {
>   a[i++] = d;
>   }
>
>
> Variant 2 is _wrong_. This is because you actually can't know without case 
> per case analysis whether 'step' is slightly above or slightly below the 
> expected value ('0.1' in this case).
>
> Because of this you can _not_ be sure how many times the loop body will be 
> executed, so in "C" you can also have an index overrun.
>
> This is a "C" FAQ.
>
> Enter "using floating point variables in loops" into your favorite web search 
> engine and read, for example, this:
>
> https://www.securecoding.cert.org/confluence/display/seccode/FLP30-C.+Do+not+use+floating+point+variables+as+loop+counters
> .

Octave is not C. Octave determines the number of elements in a range
in a way that does not have this issue:
http://hg.savannah.gnu.org/hgweb/octave/file/d95e719ef108/liboctave/Range.cc#l467
and
http://hg.savannah.gnu.org/hgweb/octave/file/d95e719ef108/liboctave/Range.cc#l59

Max Brister


reply via email to

[Prev in Thread] Current Thread [Next in Thread]