Operate with large numbers in LibreOffice BASIC - libreoffice

I'm trying to operate the MOD of a number but it returns overflow data type.
This operation works:
VariableDouble = 2147483647 - (97 * (2147483647 \ 97))
MsgBox VariableDouble
This operation returns error:
VariableDouble = 2147483648 - (97 * (2147483648 \ 97))
MsgBox VariableDouble
Shouldn't be Double data type larger than Long one? Of course, variable declaration is as follows: Dim VariableDouble As Double.
It happens the same if I use the built-in operator MOD: a MOD b.
I need to do operations with 10 digits numbers. Is there a way I can do this using BASIC? What is happening?

I found a solution making my own MOD implementation [partially] with an iterative bucle. It works —I didn't test it using numbers with a lenght greater than 50 digits, though—, so this is the code:
Dim LargeNumber As Double
Dim result As Integer
Dim dividend As Integer
Dim index As Integer
For index = 1 To Len(LargeNumber)
dividend = result & Mid(LargeNumber, index, 1)
result = dividend Mod 97
Next
It seems BASIC can't operate with large numbers, despite it can save them into variables. An another solution would be to call Python externally (API can do that). Python is awesome, however I need to keep the portability of this database for several systems (some use Windows OS so won't have Python preinstalled).

From https://wiki.openoffice.org/wiki/Using_Python_on_Windows: "If you do not customize the installation, Python-UNO bridge is installed as default on recent version." So a dependence on Python should be portable.
def big_numbers():
dividend = 12345678901234567890123456789012345678901234567890
divisor = 97
modulus = dividend % divisor
rounded_val = modulus * divisor
result = dividend - rounded_val
oDoc = XSCRIPTCONTEXT.getDocument() # the current Writer document
oVC = oDoc.getCurrentController().getViewCursor()
output = ("%d - (%d * (%d \ %d)) = %d\n" %
(dividend, divisor, dividend, divisor, result))
oDoc.getText().insertString(oVC, output, False)
g_exportedScripts = (big_numbers,)
See https://wiki.openoffice.org/wiki/Python_as_a_macro_language for how to run this code.
Related: https://forum.openoffice.org/en/forum/viewtopic.php?t=39854.

Related

convert number string into float with specific precision (without getting rounding errors)

I have a vector of cells (say, size of 50x1, called tokens) , each of which is a struct with properties x,f1,f2 which are strings representing numbers. for example, tokens{15} gives:
x: "-1.4343429"
f1: "15.7947111"
f2: "-5.8196158"
and I am trying to put those numbers into 3 vectors (each is also 50x1) whose type is float. So I create 3 vectors:
x = zeros(50,1,'single');
f1 = zeros(50,1,'single');
f2 = zeros(50,1,'single');
and that works fine (why wouldn't it?). But then when I try to populate those vectors: (L is a for loop index)
x(L)=tokens{L}.x;
.. also for the other 2
I get :
The following error occurred converting from string to single:
Conversion to single from string is not possible.
Which I can understand; implicit conversion doesn't work for single. It does work if x, f1 and f2 are of type 50x1 double.
The reason I am doing it with floats is because the data I get is from a C program which writes the some floats into a file to be read by matlab. If I try to convert the values into doubles in the C program I get rounding errors...
So, (after what I hope is a good question,) how might I be able to get the numbers in those strings, at the right precision? (all the strings have the same number of decimal places: 7).
The MCVE:
filedata = fopen('fname1.txt','rt');
%fname1.txt is created by a C program. I am quite sure that the problem isn't there.
scanned = textscan(filedata,'%s','Delimiter','\n');
raw = scanned{1};
stringValues = strings(50,1);
for K=1:length(raw)
stringValues(K)=raw{K};
end
clear K %purely for convenience
regex = 'x=(?<x>[\-\.0-9]*),f1=(?<f1>[\-\.0-9]*),f2=(?<f2>[\-\.0-9]*)';
tokens = regexp(stringValues,regex,'names');
x = zeros(50,1,'single');
f1 = zeros(50,1,'single');
f2 = zeros(50,1,'single');
for L=1:length(tokens)
x(L)=tokens{L}.x;
f1(L)=tokens{L}.f1;
f2(L)=tokens{L}.f2;
end
Use function str2double before assigning into yours arrays (and then cast it to single if you want). Strings (char arrays) must be explicitely converted to numbers before using them as numbers.

Scientific notation in MATLAB

Say I have an array that contains the following elements:
1.0e+14 *
1.3325 1.6485 2.0402 1.0485 1.2027 2.0615 1.7432 1.9709 1.4807 0.9012
Now, is there a way to grab 1.0e+14 * (base and exponent) individually?
If I do arr(10), then this will return 9.0120e+13 instead of 0.9012e+14.
Assuming the question is to grab any elements in the array with coefficient less than one. Is there a way to obtain 1.0e+14, so that I could just do arr(i) < 1.0e+14?
I assume you want string output.
Let a denote the input numeric array. You can do it this way, if you don't mind using evalc (a variant of eval, which is considered bad practice):
s = evalc('disp(a)');
s = regexp(s, '[\de+-\.]+', 'match');
This produces a cell array with the desired strings.
Example:
>> a = [1.2e-5 3.4e-6]
a =
1.0e-04 *
0.1200 0.0340
>> s = evalc('disp(a)');
>> s = regexp(s, '[\de+-\.]+', 'match')
s =
'1.0e-04' '0.1200' '0.0340'
Here is the original answer from Alain.
Basic math can tell you that:
floor(log10(N))
The log base 10 of a number tells you approximately how many digits before the decimal are in that number.
For instance, 99987123459823754 is 9.998E+016
log10(99987123459823754) is 16.9999441, the floor of which is 16 - which can basically tell you "the exponent in scientific notation is 16, very close to being 17".
Now you have the exponent of the scientific notation. This should allow you to get to whatever your goal is ;-).
And depending on what you want to do with your exponent and the number, you could also define your own method. An example is described in this thread.

convert 4 bytes to float matlab

I read from the serial port 4 bytes and I want to create a Matlab
function to convert them into a float number
for example: if I read A=[65 240 0 0] I must have 30 according to
IEEE754 standard.
- I have used Simulink block "byte unpack" but i have problems. in fact
I should read over 18 parameters. each parameters is 4 bytes
array.then I should use 18 byte unpack.
By default, Matlab will use double precision to store any new value which doesn't have a type specified. If you know you are reading byte, then the best is to collect them directly as uint8 (the unsigned byte type of Matlab) if you can (in your call to fread or equivalent).
If you cannot collect them directly as uint8 then cast them as such then use the typecast function.
A = [65 240 0 0] ; %// Collected bytes
A = uint8(A) ; %// cast them to "uint8" if they are not already
Afloat = typecast( A , 'single') ; %// cast the 4 bytes as a 32 bit float
wait a minute:
Afloat =
8.6186862e-41
oops, it seems the byte ordering used by your collection mechanism is the opposite as the one used by Matlab. No problem, you can just change the endianness by "flipping" the byte array.
So instead, use:
>> Afloat = typecast( fliplr(A) , 'single')
Afloat =
30
success :)
You can also look at the function swapbytes to manage the endianess.
To avoid reinventing the wheel, simply use
A = fread(obj,size,'precision')
as described in documentation
For example,
[A,count] = fread(obj, 18, 'float32');
should read 18 4 byte floats.

FORTRAN 90 separating digits in an integer

Hej folks, I'm quite the beginner in programming but I read my share of stackoverflow pages, and googled a bit as well, still can't figure if the following is even possible in FORTRAN 90.
I'm trying to isolate the digits in an integer, to point where the hurdle is, consider the following idea :
INTEGER :: n, mult, add
READ *, n ! n = 8
mult = n*2 ! = 16
add = ??? ! where I want to add 1 + 6
Another way, I trust that this will be obvious to anyone reading the code:
INTEGER FUNCTION sum_digits(num)
INTEGER, INTENT(in) :: num
INTEGER, DIMENSION(:), ALLOCATABLE :: digs
INTEGER :: num_digits, ix, rem
num_digits = FLOOR(LOG10(REAL(num))+1)
ALLOCATE(digs(num_digits))
rem = num
DO ix = 1, num_digits
digs(ix) = rem - (rem/10)*10 ! Take advantage of integer division
rem = rem/10
END DO
sum_digits = SUM(digs)
END FUNCTION sum_digits
I've subjected this to a quick series of obvious tests and it has passed all 4 of them. If you find a case for which it doesn't work, fix it. And if you want the array of digits returned, modify the function to return that. If you want it to work for negative integers too throw in ABS() at an appropriate place.
one way to pull off the 'ith' place digit is:
n/10**i-10*(n/10**(i+1))
so for your example:
n-10*(n/10) + n/10-10*(n/100)

What are # and : used for in Qbasic?

I have a legacy code doing math calculations. It is reportedly written in QBasic, and runs under VB6 successfully. I plan to write the code into a newer language/platform. For which I must first work backwards and come up with a detailed algorithm from existing code.
The problem is I can't understand syntax of few lines:
Dim a(1 to 200) as Double
Dim b as Double
Dim f(1 to 200) as Double
Dim g(1 to 200) as Double
For i = 1 to N
a(i) = b: a(i+N) = c
f(i) = 1#: g(i) = 0#
f(i+N) = 0#: g(i+N) = 1#
Next i
Based on my work with VB5 like 9 years ago, I am guessing that a, f and g are Double arrays indexed from 1 to 200. However, I am completely lost about this use of # and : together inside the body of the for-loop.
: is the line continuation character, it allows you to chain multiple statements on the same line. a(i) = b: a(i+N) = c is equivalent to:
a(i)=b
a(i+N)=c
# is a type specifier. It specifies that the number it follows should be treated as a double.
I haven't programmed in QBasic for a while but I did extensively in highschool. The # symbol indicates a particular data type. It is to designate the RHS value as a floating point number with double precision (similar to saying 1.0f in C to make 1.0 a single-precision float). The colon symbol is similar to the semicolon in C, as well, where it delimits different commands. For instance:
a(i) = b: a(i+N) = c
is, in C:
a[i] = b; a[i+N] = c;