FORTRAN compiler warning: obsolete arithmetic IF statement - gfortran

I have a gfortran error:
Warning: Obsolete: arithmetic IF statement at (1)
What does this mean? In the source (old source):
66 s12 = max(epsilon, s1 + s2)
c Then execution will go to label 13. Will this stop any further problems?
if (s12 - 1.0) 13, 13, 12
13 z = s1 / s12

Arithmetic if is a peculiar feature of FORTRAN
it works as follows.
IF (expr) label1, label2, label3
If the value of the expression is
less than 0, jump to label1
equal to 0, jump to label2
greater than 0, jump to label3
In newer FORTRAN standards this feature is obsolete
In your code you can replace it with
IF (s12 - 1.0 .gt. 0 ) GOTO 12
13 z = s1 / s12

Check here:
http://www.ibiblio.org/pub/languages/fortran/ch1-5.html
"The Arithmetic IF is considered harmful."
Your statement,
if (s12 - 1.0) 13, 13, 12 is an Arithmetic IF, and is considered bad programming.

Quoth Wikipedia:
"..the Fortran statement defines three different branches depending on whether the result of an expression was negative, zero, or positive, in said order..."
"..was finally labeled obsolescent in Fortran 90."

Related

2^65 modulo 101 incorrect anwser

This code checks that the value a maps uniquely for the values 1 to 100 using the formula (a^x) % 101
local function f(a)
found = {}
bijective = true
for x = 1, 100 do
value = (a^x) % 101
if found[value] then
bijective = false
break
else
found[value] = x
end
end
return bijective
end
However does not produce the expected result.
it maps 2^65 % 101 to 56, which matches the value produced by 2^12 % 101 and I get a false result, however the correct value for 2^65 % 101 is 57 and 2 actually should produce all unique values resulting in a true result.
The error described above is specifically on Lua 5.1, is this just a quirk of Lua's number typing? Is there a way to make this function work correctly in 5.1?
The error described above is specifically on Lua 5.1, is this just a quirk of Lua's number typing? Is there a way to make this function work correctly in 5.1?
First of all, this is not an issue with Lua's number typing since 2^65, being a (rather small) power of two, can be represented exactly by the double precision since it uses an exponent-mantissa representation. The mantissa can simply be set to all zeroes (leading one is implicit) and the exponent must be set to 65 (+ offset).
I tried this on different Lua versions and PUC Lua 5.1 & 5.2 as well as LuaJIT have the issue; Lua 5.3 (and presumably later versions as well) are fine. Interestingly, using math.fmod(2^65, 101) returns the correct result on the older Lua versions but 2^65 % 101 does not (it returns 0 instead).
This surprised me so I dug in the Lua 5.1 sources. This is the implementation of math.fmod:
#include <math.h>
...
static int math_fmod (lua_State *L) {
lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
return 1;
}
this also is the only place where fmod from math.h appears to be used. The % operator on the other hand is implemented as documented in the reference manual:
#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b))
in src/luaconf.h. You could trivially redefine it as fmod(a,b) to fix your issue. In fact Lua 5.4 does something similar and even provides an elaborate explanation in its sources!
/*
** modulo: defined as 'a - floor(a/b)*b'; the direct computation
** using this definition has several problems with rounding errors,
** so it is better to use 'fmod'. 'fmod' gives the result of
** 'a - trunc(a/b)*b', and therefore must be corrected when
** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a
** non-integer negative result: non-integer result is equivalent to
** a non-zero remainder 'm'; negative result is equivalent to 'a' and
** 'b' with different signs, or 'm' and 'b' with different signs
** (as the result 'm' of 'fmod' has the same sign of 'a').
*/
#if !defined(luai_nummod)
#define luai_nummod(L,a,b,m) \
{ (void)L; (m) = l_mathop(fmod)(a,b); \
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
#endif
Is there a way to make this function work correctly in 5.1?
Yes: The easy way is to use fmod. This may work for these particular numbers since they still fit in doubles due to the base being 2 and the exponent being moderately small, but it won't work in the general case. The better approach is to leverage modular arithmetics to keep your intermediate results small, never storing numbers significantly larger than 101^2 since (a * b) % c == (a % c) * (b % c).
local function f(a)
found = {}
bijective = true
local value = 1
for _ = 1, 100 do
value = (value * a) % 101 -- a^x % 101
if found[value] then
bijective = false
break
else
found[value] = x
end
end
return bijective
end

How to present negative number in bitvector?

The title says it all. I try to present -1 as the following: (_ bv-1 32), and z3 complains.
How do I present constraint such as 3x - 5y <= 10 in bit vector? For some reason, I do not want to use linear integer.
This is usually done via two's complement encoding. The short version is,
-x = flip(x) + 1
where flip(x) simply flips all the bits in x.

How to create Fibonacci sequence in Lua?

I wrote a small script that creates Fibonacci sequence and returns a sum of all even integers.
function even_fibo()
-- create Fibonacci sequence
local fib = {1, 2} -- starting with 1, 2
for i=3, 10 do
fib[i] = fib[i-2] + fib[i-1]
end
-- calculate sum of even numbers
local fib_sum = 0
for _, v in ipairs(fib) do
if v%2 == 0 then
fib_sum = fib_sum + v
end
end
return fib_sum
end
fib = even_fibo()
print(fib)
The function creates the following sequence:
1, 2, 3, 5, 8, 13, 21, 34, 55
And returns the sum of its even numbers: 44
However, when I change the stop index from 10 to 100, in for i=3, 100 do the returned sum is negative -8573983172444283806 because the values become too big.
Why is my code working for 10 and not for 100?
Prior to version 5.3, Lua always stored numbers internally as floats. In 5.3 Lua numbers can be stored internally as integers or floats. One option is to run Lua 5.2, I think you'll find your code works as expected there. The other option is to initialize your array with floats which will promote all operations on them in the future to floats:
local fib = {1.0, 2.0}
Here is a hack written in hindsight.
The code exploits the mathematical fact that the even Fibonacci numbers are exactly those at indices that are multiple of 3.
This allows us to avoid testing the parity of very large numbers and provides high-order digits that are correct when you do the computation in floating-point. Then we redo it looking only at the low-order digits and combine the results. The output is 286573922006908542050, which agrees with WA. Values of d between 5 and 15 work fine.
a,b=0.0,1.0
s=0
d=10
for n=1,100/3 do
a,b=b,a+b
a,b=b,a+b
s=s+b
a,b=b,a+b
end
h=string.format("%.0f",s):sub(1,-d-1)
m=10^d
a,b=0,1
s=0
for n=1,100/3 do
a,b=b,(a+b)%m
a,b=b,(a+b)%m
s=(s+b)%m
a,b=b,(a+b)%m
end
s=string.format("%0"..d..".0f",s)
print(h..s)

What does ^ mean in objective c ios? [duplicate]

This question already has answers here:
What does the ^ operator do to a BOOL?
(7 answers)
Closed 10 years ago.
Sorry to ask such a simple question but these things are hard to Google.
I have code in iOS which is connected to toggle which is switching between Celsius and Fahrenheit and I don't know what ^ 1 means. self.celsius is Boolean
Thanks
self.celsius = self.celsius ^ 1;
It's a C-language operator meaning "Bitwise Exclusive OR".
Wikipedia gives a good explanation:
XOR
A bitwise XOR takes two bit patterns of equal length and performs the
logical exclusive OR operation on each pair of corresponding bits. The
result in each position is 1 if only the first bit is 1 or only the
second bit is 1, but will be 0 if both are 0 or both are 1. In this we
perform the comparison of two bits, being 1 if the two bits are
different, and 0 if they are the same. For example:
0101 (decimal 5)
XOR 0011 (decimal 3)
= 0110 (decimal 6)
The bitwise XOR may be used to invert selected bits in a register
(also called toggle or flip). Any bit may be toggled by XORing it with
1. For example, given the bit pattern 0010 (decimal 2) the second and fourth bits may be toggled by a bitwise XOR with a bit pattern
containing 1 in the second and fourth positions:
0010 (decimal 2)
XOR 1010 (decimal 10)
= 1000 (decimal 8)
It's the bitwise XOR operator (see http://www.techotopia.com/index.php/Objective-C_Operators_and_Expressions#Bitwise_XOR).
What it's doing in this case is switching back and forth, because 0 ^ 1 is 1, and 1 ^ 1 is 0.
It's an exclusive OR operation.

Lua: converting from float to int

Even though Lua does not differentiate between floating point numbers and integers, there are some cases when you want to use integers. What is the best way to covert a number to an integer if you cannot do a C-like cast or without something like Python's int?
For example when calculating an index for an array in
idx = position / width
how can you ensure idx is a valid array index? I have come up with a solution that uses string.find, but maybe there is a method that uses arithmetic that would obviously be much faster. My solution:
function toint(n)
local s = tostring(n)
local i, j = s:find('%.')
if i then
return tonumber(s:sub(1, i-1))
else
return n
end
end
You could use math.floor(x)
From the Lua Reference Manual:
Returns the largest integer smaller than or equal to x.
Lua 5.3 introduced a new operator, called floor division and denoted by //
Example below:
Lua 5.3.1 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>12//5
2
More info can be found in the lua manual
#Hofstad is correct with the math.floor(Number x) suggestion to eliminate the bits right of the decimal, you might want to round instead. There is no math.round, but it is as simple as math.floor(x + 0.5). The reason you want to round is because floats are usually approximate. For example, 1 could be 0.999999996
12.4 + 0.5 = 12.9, floored 12
12.5 + 0.5 = 13, floored 13
12.6 + 0.5 = 13.1, floored 13
local round = function(a, prec)
return math.floor(a + 0.5*prec) -- where prec is 10^n, starting at 0
end
why not just use math.floor()? it would make the indices valid so long as the numerator and denominator are non-negative and in valid ranges.

Resources