I have two CGFloat values, and want to calculate the modulo result. Or in other words: I want to know what's left if valueA is placed as much as possible into valueB.
So I just tried:
CGFloat moduloResult = valueB % valueA;
the compiler complains about the % and tells me: "invalid operands to binary %". Any idea?
% is for int or long, not float or double.
You can use fmod() or fmodf() from <math.h> instead.
Better is <tgmath.h> as suggested by the inventor of CGFloat.
If I remember correctly modulo requires 2 ints as its input so you'd need something like:
CGFloat moduloResult = (float)((int)valueB % (int)valueA);
Assuming that valueB and valueA are both floats
Related
I am trying to convert from float/double to fixed point notation, for instance let's use the number x = 0.39 as input.
I would like to convert x into its unsigned 16 bits fixed-point counterpart, to do so in C++ I am accustomed to use the expression xFixedPoint = round(x*2^(16)), this will make that the fixed point version of 0.39 is 25559.
However, I cannot get that result if I make use of num2fixpt function, I am using it in the following way
num2fixpt(0.39, ufix(16))
ans = 0
What am I doing wrong when using num2fixpt function?
>> num2fixpt(0.39,ufix(16),2^-2)
ans =
0.2500
So you also need to scale to get the integer representation, i.e. 2^16*num2fixpt(...)
A floating point is usually given in decimal notation, e.g. 1.25. However for binary the same value could be 1.01.
It is possible to use such binary notation to specify floating point values in MATLAB?
As #Ander Biguri said that is not floating point (IEEE-754 double see here).
However, if you want to represent binary the way you suggest in your question you could do something the example below. In my example you have to specify how many bits you want to represent the binary integer & fractional portions and it outputs as a char array:
num2Convert = 1.25;
numInt = 5; %Number of bits for interger portion
numFrac = 5; %Number of bits for fraction portion
dInt = num2cell(fix(rem(num2Convert*pow2(-numInt+1:0),2)));
% More simply you could just do dec2bin(fix(num2convert)); for the int portion...
% but I wanted to be consistent with the fractional portion
dFrac = num2cell(fix(rem( rem(num2Convert,1)*pow2(1:numFrac),2)));
binString = [sprintf('%d',dInt{:}),'.',sprintf('%d',dFrac{:})]
binString =
00001.01000
Now if you want to see the binary representation of the underlying double precision float you could do this:
binString = dec2bin(typecast(num2Convert,'uint64'),64)
binString =
0011111111110100000000000000000000000000000000000000000000000000
e.g. x = 12.354
I want to get 354 from x. For this I tried this equation,
y = x - floor(x)
But this generates 0.354 which is not my requirement.
So, how can I do it?
The generic idea as pointed out by #JoachimPileborg looks like this in MATLAB:
x = 12.354;
str = num2str(x);
idx = find(str=='.');
substr = str(idx+1:end);
y = str2num(substr);
A generic solution that should work with all programming languages, is to convert the number to a string, then take the sub-string from after the decimal point (or comma) and convert it to an integer.
An alternative (possibly faster) to m.s. 's answer. Notice the 'shift' due to this approach effectively removing the zeroes to the right of the decimal point.
yfoo = 34.00267400;
yfoo = yfoo- floor(yfoo); % .00267400;
% internal rep of number ignores trailing zeros.
yrab = str2num(strrep(num2str(yfoo),'.','')); %2674
I am trying to calculate two random numbers constrained to a particular range using arc4random and the % operator.
I am not able to figure out why the compiler is complaining when calculating (x1,y1) when almost the same thing is done for x2,y2 except for using an int literal directly.
Any ideas?
The code sample is:
import Darwin
let w1:Int = 223
// error: Could not find an overload for '%' that accepts the supplied arguments
let (x1,y1) =
(
arc4random() % w1,
arc4random() % 345
)
let (x2,y2) =
(
arc4random() % 223,
arc4random() % 345
)
That's due to Swift's enforcement of type safety.
arc4random() returns a UInt32, while you've declared w as an Int, so a compiler error is thrown. That'd also happen if you had tried to sum or divide w and the result of calling arc4random(), so the behaviour is not exclusive to the remainder operator.
You have a few options, depending on how you intend to use the resulting value of the operation.
You can declare w1 as an UInt32 in the first place:
var w1: UInt32 = 223
Or, you can cast it to an UInt32 before performing the remainder operation:
arc4random() % UInt32(w1)
Finally, you can initialize an Int from the result of the arc4random() call and use it with the Int version of w1
Int(arc4random()) % w1
Note that this behaviour does not apply to literals.
From the Apple provided iBook:
The rules for combining numeric constants and variables are different
from the rules for numeric literals. The literal value 3 can be added
directly to the literal value 0.14159, because number literals do not
have an explicit type in and of themselves. Their type is inferred
only at the point that they are evaluated by the compiler.
That's why you don't see the same problem occur when doing arc4random() % 345.
You need to use the same type for both variables, to wit:
let w1:UInt32 = 223
Should solve the problem
The error is because you are trying to use an Int and a UInt32
I'm having some problems with MATLAB and 64-bit integers. I want to have a mask equivalent to 2^63-1 (all ones except the MSB), but MATLAB just seems to round everything.
>> mask_fraction = uint64(9223372036854775807)
mask_fraction = 9223372036854775808 % This is 2^63 again, not 2^63-1!
Similarly,
>> uint64(2^63)
ans = 9223372036854775808
>> uint64(2^63-1)
ans = 9223372036854775808
Another one of my attempts simply doesn't work:
>> uint64(2^63) - 1
??? Undefined function or method 'minus' for input arguments of type 'uint64'.
Thoughts?
#BasSwinckels correctly points out one issue. I'll address another.
The first non-exactly representable double-precision floating point integer is 2^53+1. When you pass in expressions to the uint64 function they are evaluated as doubles before being cast to uint64. If these expressions evaluate to a double precision integer that is not exactly representable, you'll see behavior like you described. This is exactly why uint64(2^63-1) and uint64(2^63) both return 9223372036854775808. All powers of two can be safely represented in double precision, so uint64(2^63)-1 or uint64(2^63)-uint64(1) are what you should use (once you figure out your other issue).
I don't see the problems you report. On my computer (Matlab R2012b on 64-bit Ubuntu12.04):
>> mask_fraction = uint64(9223372036854775807)
mask_fraction =
9223372036854775807
>> uint64(2^63) - 1
ans =
9223372036854775807
Do you maybe run an older Matlab version?
I also find the 'undefined function or method minus ...' error a bit suspicious. Do you have uint64 aliased to some other function? Try clear uint64 first ...
It seems from the other comments that Matlab does not implement the minus method for class uint64 in some older versions. The ugly workaround below works for me in R2011b:
>> bitset(intmax('uint64'), 64, 0)
ans =
9223372036854775807
I don't know if it is supported by the version of MATLAB you are using but this might help
mask_fraction = uint64(intmax('int64'))
which returns
mask_fraction =
9223372036854775807
2^63-1 is the maximum value for int64.