Trouble with large numbers while using <iomanip> larger in cpp - c++

While sorting and displaying big numbers you usually end up displaying the large numbers in enotation. I was trying to display the whole number by using the <iomanip> library in cpp and it fails for very large numbers.
//big sorting
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
int main()
{
int n;
cin>>n;
double arr[n];
for (int i = 0;i < n; i++)
cin>>arr[i];
sort(arr, arr+n);
cout<<fixed<<setprecision(0);
for (int i = 0;i < n; i++)
cout<<arr[i]<<endl;
}
Input:
31415926535897932384626433832795
1
3
10
3
5
Expected output:
1
3
3
5
10
31415926535897932384626433832795
Actual output:
1
3
3
5
10
31415926535897933290036940242944
The last digit is getting messed up.

The double type precision is only 15 decimal digits, so very large whole numbers can't be expressed in double type without loss of precison.

Read more about C++, perhaps the C++11 standard n3337.
Read also the documentation of your C++ compiler, e.g. GCC (invoked as g++) or Clang (invoked as clang++). Read of course a good C++ programming book, since C++ is a very difficult programming language. Use C++ standard containers and smart pointers.
Large numbers does not fit natively in a computer memory (or in its registers). For example, with C++ code compiled by GCC on Linux/x86-64, an int has just 32 bits.
Consider using arbitrary precision arithmetic. You might be interested by GMPlib.
Floating point numbers are weird. Be sure to read the famous floating-point-gui.de website, and see also this answer.
#include<bits/stdc++.h>
is wrong since non-standard. Take the habit of #include-ing only headers needed by your translation unit, except if you use pre-compiled headers.
Take some time to read more about numbers and arithmetic. Some notion of modular arithmetic is incredibly useful when programming: a lot of computers are computing modulo 232 or 264.
Study for inspiration the C++ source code of existing open source software (e.g. on github or gitlab, including FLTK). If you use Linux, its fish-shell has a nice C++ code. You could even glance inside the source code of GCC and of Clang, both being nice C++ open source compilers.
In practice, read also about build automation tools such as GNU make (free software coded in C) or ninja (open source tool coded in C++).
Don't forget to use a version control system (I recommend git).
Read How to debug small programs.
Enable all warnings and debug info when compiling your C++ code (with GCC, use g++ -Wall -Wextra -g).
Read of course the documentation of your favorite debugger.
I am a happy user of GDB.
Consider using static program analysis tools such as the Clang static analyzer or Frama-C++.

Related

Compiling C++ code to .EXE which returns double [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 5 years ago.
I am working with a MATLAB optimization platform for black-box cost functions (BBCF).
To make the user free-handed, the utilized BBCF can be any executable file which inputs the input parameters of BBF and must output (return) the cost value of BBCF, so that the MATLAB BBCF optimizer finds the best (least cost) input parameter.
Considering that, from the one hand, my BBCF is implemented in C++, and from the other hand the cost value is a double (real number), I need to compile my code to an EXE file that outputs (returns) double.
But, to the best of my knowledge, when I compile a C++ code to EXE, it "mainly" compiles the main() function and its output is the output of main() function (i.e. 0 if running successful!).
An idea could be using a main function that returns double, and then, compiling such main() to EXE but, unfortunately, it is not possible in C++ (as explained in this link or as claimed in the 3rd answer of this question to be a bug of C++ neither of which are business of this question).
Can anyone provide an idea by which the EXE compiled form of a C++ code, outputs (returns) a double value?
This is not 'a bug in C++' (by the way, the bug might be in some C++ compiler, not in the language itself) - it's described in the standard that main() should return an integer:
http://en.cppreference.com/w/cpp/language/main_function
Regarding how to return a non-int from an executable, there are a couple of ways to do that. Two simplest (in terms of how to implement them) solutions come to my mind:
Save it to a file. Then either monitor that file in Matlab for changes (e.g. compare timestamps) or read after each execution of your EXE file depending on how you're going to use it. Not very efficient solution, but does the job and probably the performance penalty is negligible to that of your other calculations.
If you are fine with your cost value losing some numerical accuracy, you can just multiply the double value by some number (the larger this number, the more decimal places you will retain). Then round it, cast it to an int, have it returned from main(), cast it back to double in matlab and divide by the same number. The number used as the multiplier should be a power of 2 so that it doesn't introduce additional rounding errors. This method might be particularly useful if your cost value takes the values limited to the range [0, 1] or if you can normalize it to these values and you know that variations less than some threshold are not important.
In English, 'shall' is an even stronger imperative than 'must'.
Making a change like this would require changes to the operating system and shell. Such changes are unlikely to happen.
The easiest way to pass a double return would to to write it to standard output. Alternatively there are several methods available for interprocess communication.

Wierd output after sorting

I am getting weird output after sorting
If giving input using scanf the line is causing error. The output is in some weird arrangement. (I have commented the line)
If I use cin the output is fine. Also the problem is not present in online compilers. Same thing is happening on different computers.
Eg if I input
5
23 44 32 2 233
the output is
32 23 233 2 44
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
unsigned long long int n=0,i=0;
// cin>>n;
scanf("%llu",&n);
unsigned long long int arr[n];
for(i=0;i<n;i++)
{
// cin>>arr[i]; //if use this no error but if use next line it is
scanf("%llu",&arr[i]); //causing error
}
sort(arr,arr+n);
for(i=0;i<n;i++)
{
// cout<<arr[i]<<" ";
printf("%llu ",arr[i]);
}
return 0;
}
If using cin helps, then probably %llu is wrong flag for given pointer. Check documentation for your compiler and see what long long means for it and check printf/scanf doc of your library.
There is all sorts of possible explanations. Most likely is a compiler (or library or runtime) in which scanf() doesn't properly support the %llu format with long long unsigned types.
long long types were not formally part of C before the 1999 standard, and not part of C++ before (from memory) the 2011 standard. A consequence is that, depending on age of your compiler, support varies from non-existent to partial (what you are seeing) to complete.
Practically, because of how C++ stream functions (operator>>() etc) are organised, it is arguably easier to change the C++ streaming to support a new type (like long long unsigned) than it is to change C I/O. C++ streams, by design, are more modular (operator>>() is a function overloaded for each type, so it is easier to add support for new types without breaking existing code that handles existing types). C I/O functions (like scanf()) are specified in a way that allows more monolithic implementations - which means that, to add support of new types and format specifiers, it is necessary to change/debug/verify/validate more existing code. That means it can reasonably take more effort - and therefore time - to change C I/O than C++ I/O. Naturally, YMMV in this argument - depending on skill of the library developer. But, in practice, it is a fair bet that an implementation of standard C++ streams is more modular - and therefore easier to maintain - by various measures than the C I/O functions.
Also, although you didn't ask, the construct unsigned long long int arr[n] where n is a variable is invalid C++. You are better off using a standard container, like std::vector.
What probably happens is that %llu is wrong for your compiler.
For example, if you compile your program with g++ in MinGW using the -Wall flag in order to get all the warnings, you get this:
a.cpp: In function 'int main()':
a.cpp:16:20: warning: unknown conversion type character 'l' in format [-Wformat=]
scanf("%llu",&n);
So the compiler will ignore the extra l and scan the input as %lu.
If you are compiling on a 32bit system, a long integer will most probably be 32bits long, i.e. 4 bytes.
So the number scanned will occupy 4 bytes in your memory and since the arr[n] array is a long long array, i.e. each element is 64bits - 8 bytes, only the first 4 bytes of each element will be written by scanf.
Assuming you are on a little endian system, these 4 bytes will be the least significant part of the long long element. The most significant part will not be written and will probably contain garbage.
The sort algorithm though, will use the full 8 bytes of each element for the sorting and so will sort the array using the garbage in the most significant part of each element.
Since you are using code blocks on a Windows 32bit system, try replacing all occurrences of "%llu" with "%I64u".

Same C source, different output

I've found an old C source code that I've implemented several years ago, and also its compiled binary executable.
In a comment I wrote that the compilation command was:
gcc -O3 source.c -o executable -lm
So I recompiled it but the new executable is different (in size) from the old one.
In fact if I run the new and the old executable they give me different results: the old executable returns the same result returned years ago, while the new one returns a different result.
My goal would be to be able to recompile the source and obtain the same executable as the old (or at least an executable that produce exactly the same result).
I'm sure that I run the two programs with the same parameters, and that the code does not use threads at all. The only particular thing is that I need random integers, but I use my own function to produce them just in case to be sure that the sequence of random numbers is always the same (and of course I always use the same seed).
unsigned int seed = 0;
void set_srand(unsigned int aseed) {
seed = aseed;
}
int get_rand() {
seed = seed * 0x12345 + 0xABC123;
int j = (seed >> 0x10) & RAND_MAX;
return j;
}
(I thought this was copied from some library).
So what can it be? Maybe the OS where the compilation is done (the original one was under WinXP, now I'm trying under both Win7 and Ubuntu), but I've always only used MinGW. So maybe the version of MinGW? If this is the case, I'm in trouble because I don't remember which version I've used several years ago.
Libraries that I use:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
What I do are only string operations and computation like pow(), sqrt(), multiplication and plus/minus, in order to apply an heuristic optimization algorithm to solve approximately an NP-hard problem: the results are both solutions of the problem, but their fitness differs.
The first thing I would check is the size of int. Your code relies on overflow and the size of integers may matter.
Your code seems to imply that int is at least 32 bit (you use 0x0x269EC3) but maybe you are now compiling with int at 64 bit.
I wouldn't worry about executable file, it's very unlikely you get the same size by two different compilers.

Handle arbitrary length integers in C++

Can someone tell me of a good C++ library for handling (doing operations etc...) with arbitrarily large numbers (it can be a library that handles arbitrary precision floats too, but handling integers is more important)?
Please only refer to libraries that YOU used and tell me how did you managed to set it up and pick it up, maybe with a very minimalistic example or something (basically if the mentioned library lacks good documentation provide some input of your own).
For the record I'm using Windows 7 on an x64 machine, CodeBlocks as my IDE, and the latest MinGW as the compiler.
Libraries I tried:
vlint (not enough operations, works fine for small stuff though)
bigint (easy to set it up, compile errors and not much documentation (from which errors might be derived))
ttmath (seemed promising, compiled some BIG example programs and ran after some fixes because of compiling errors, incomprehensible syntax because of virtually no documentantion)
gmp (couldn't even set it up)
p.s. Removed the 'rant part of the question' that basically explained why I'm asking something that was asked a lot of times on Stackoverflow so people would read it to the end.
--> UPDATE
So i picked an answer that wasn't a direct answer to my initial question but helped me a lot to solve this and i will post some of my findings to help other c++ newbies like me to get started working with very big numbers without struggling with libraries for days like i did in an easy step by step micro-guide.
Stuff i was using (keep this in mind to follow the guide):
Windows 7 Ultimate x64
Amd k10 x64 (some libraries won't work with this, others will behave differently, others are custom taylored to amd k10 so this won't only help you with the library i used but possibly with others too)
Code::Blocks 10.05 the version without MinGW included, file name "codeblocks-10.05-setup.exe" (installed on C:\Program Files (x86)\CodeBlocks)
MinGW packages (binutils-2.15.91-20040904-1.tar.gz gcc-core-3.4.2-20040916-1.tar.gz gcc-g++-3.4.2-20040916-1.tar.gz mingw-runtime-3.11.tar.gz w32api-3.8.tar.gz) extracted on C:\MinGW
TTMath 0.9.2 file name "ttmath-0.9.2-src.tar.gz" unzipped and copied the folder "ttmath" to the folder "C:\CPPLibs" (which is the folder where i put my c++ libraries into)
What to do to set it all up
Go to Code:Blocks > Settings > Compiler and Debugger (My compiler was detected automatically here. If this doesn't happen with you, on "Selected Compiler" select "GNU GCC Compiler" and click "Set as Default" then on "Toolchain Exectables" on "Compilers installation directory you can choose the installation directory of the compiler or attempt to auto-detect" and with that sorted on "C++ Compiler" select or write "mingw32-g++.exe". If this happens to you just do this, on "Selected Compiler" select "GNU GCC Compiler" and click "Set as Default").
Without leaving "Code:Blocks > Settings > Compiler and Debugger" and with the above sorted out, go to "Search Directories" and then "Compiler" click "Add" and choose the folder where you store your libraries or where you put your "ttmath" folder (in my case C:\CPPLibs) then go to "Linker" and do the same thing.
To start coding with the "ttmath" library you have to put this line #include <ttmath/ttmath.h> before the main function (NOTE: If you use a 64bit system you WILL get a lot of errors if you don't also put this line #define TTMATH_DONT_USE_WCHAR BEFORE this line #include <ttmath/ttmath.h>, i was struggling with this crap until i found the fix that some other guy that was also struggling found and posted on the web and it worked for me) p.s. i think it's only for 64bit systems but if you do get errors just because of including the "ttmath.h" header file it's most likely because of that.
Declaring variables that will have big integer values has to be done like so: ttmath::UInt<n> a,b,c; where "a,b,c" are your variables and "n" is the size of the numbers you can store in the variables in this form "2^(32*n)-1" for 32bit systems and this form "2^(64*n)-1" for 64bit systems
Assigning values to variables if you do this a = 333; (and the number in place of 333 is bigger than the "long int" standard data type on c++) it won't compile because assigning values to variables like this independently of the size you specified earlier the integer can be just as big as the the "long int" standard data type on c++ (i figured this one on my own, the hard way), also even if you use a value that is smaller and it compiles alright and then you run your program and it tries to write to this variable a bigger number than the number that the mentioned "long int" standard data type can handle, then your math is going to be wrong so watch this: to assign a value to a variable the right way you have to assign it like so a = "333"; (yes i know you are pretty much treating it as a string this way but it will do operations just fine with no problems whatsoever and if you decide to "cout" the variable it will never be an exponential or scientific notation result like you get using standard integer data types without being coupled with some 'extra statements' to display the number just right)
p.s. Using this simple rules to work with integers and this library i computed fibonacci numbers up to the 100.000th number with a simple program (that took like 3min to code) in 15 to 20 seconds and the number occupied like 3 pages so besides being a practical library once you get to know how it works (that you had virtually no help before, some samples of the ttmath website are pretty misleading, but now you do have some help) it also seems pretty efficient, i confirmed that the 100.000th number is probably right because i increased the size (the "n") from 10000 to 50000 and the number retained the size and the initial and final digits were the same. This is the source code i used, i used a VERY BIG number for the integer size just to test, i didn't actually bothered to see on what lenght the program would start do do stuff wrong but i know that the lenght of up to the 10.000th fibonacci number won't surpass the lenght that i defined because before this i made the program 'cout' every result until it got to 10.000th and it was always growing. I also checked the first numbers of the sequence before when i paused the program and i was seeing the 'digits grow' and confirmed the first fibonacci numbers of the sequence and they were correct. NOTE: This source code will only display the number of the fibonacci sequence that you want to know, it will only show you the numbers "growing" if you uncomment the commented lines.
#define TTMATH_DONT_USE_WCHAR
#include <ttmath/ttmath.h>
#include <iostream>
using namespace std;
int main () {
int fibonaccinumber;
cin >> fibonaccinumber;
cin.ignore();
ttmath::UInt<10000> fibonacci1,fibonacci2,fibonacci3;
fibonacci1 = 1;
fibonacci2 = 1;
//cout << "1. " << fibonacci1 << "\n2. " << fibonacci2 << "\n";
for(int i=3;i<=fibonaccinumber;i++)
{fibonacci3 = fibonacci1 + fibonacci2;
// cout << i << ". " << fibonacci3 << "\n";
fibonacci1=fibonacci2;
fibonacci2=fibonacci3;}
cout << "the " << fibonaccinumber << "th fibonacci number is " << fibonacci2;
string endprog;
getline(cin,endprog);
return 0;}
I didn't tinkered with arbitrary precision floats of this lbrary yet but when i do i will continue to expand this guide if i see that people are interested in it, thanks for all the comments and answers.
The official site (http://www.ttmath.org/) has samples of using integers (ttmath::Int<2> a,b,c;) and floating points (ttmath::Big<1,2> a,b,c;) both. Just treat these like high precision int/float without members and everything should be fine. If the error remains, can you post the full error message, and the lines of code that it errored on?
The Boost.Multiprecision library supports arbitrarily long integers, real numbers and ratios. It also allows you to use different back-ends which have different performance characteristics and licensing terms.
A few possibilities are MIRACL, NTL and LIP.

How much is 32 kB of compiled code

I am planning to use an Arduino programmable board. Those have quite limited flash memories ranging between 16 and 128 kB to store compiled C or C++ code.
Are there ways to estimate how much (standard) code it will represent ?
I suppose this is very vague, but I'm only looking for an order of magnitude.
The output of the size command is a good starting place, but does not give you all of the information you need.
$ avr-size program.elf
text data bss dec hex filename
The size of your image is usually a little bit more than the sum of the text and the data sections. The bss section is essentially compressed because it is all 0s. There may be other sections which are relevant which aren't listed by size.
If your build system is set up like ones that I've used before for AVR microcontrollers then you will end up with an *.elf file as well as a *.bin file, and possibly a *.hex file. The *.bin file is the actual image that would be stored in the program flash of the processor, so you can examine its size to determine how your program is growing as you make edits to it. The *.bin file is extracted from the *.elf file with the objdump command and some flags which I can't remember right now.
If you are wanting to know how to guess-timate how your much your C or C++ code will produce when compiled, this is a lot more difficult. I have observed a 10x blowup in a function when I tried to use a uint64_t rather than a uint32_t when all I was doing was incrementing it (this was about 5 times more code than I thought it would be). This was mostly to do with gcc's avr optimizations not being the best, but smaller changes in code size can creep in from seemingly innocent code.
This will likely be amplified with the use of C++, which tends to hide more things that turn into code than C does. Chief among the things C++ hides are destructor calls and lots of pointer dereferencing which has to do with the this pointer in objects as well as a secret pointer many objects have to their virtual function table and class static variables.
On AVR all of this pointer stuff is likely to really add up because pointers are twice as big as registers and take multiple instructions to load. Also AVR has only a few register pairs that can be used as pointers, which results in lots of moving things into and out of those registers.
Some tips for small programs on AVR:
Use uint8_t and int8_t instead of int whenever you can. You could also use uint_fast8_t and int_fast8_t if you want your code to be portable. This can lead to many operations taking up only half as much code, because int is two bytes.
Be very aware of things like string and struct constants and literals and how/where they are stored.
If you're not scared of it, read the AVR assembly manual. You can get an idea of the types of instructions, and from that the type of C code that easily maps to those instructions. Use that kind of C code.
You can't really say there. The length of the uncompiled code has little to do with the length of the compiled code. For example:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
std::vector<std::string> strings;
strings.push_back("Hello");
strings.push_back("World");
std::sort(strings.begin(), strings.end());
std::copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(std::cout, ""));
}
vs
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
std::vector<std::string> strings;
strings.push_back("Hello");
strings.push_back("World");
for ( int idx = 0; idx < strings.size(); idx++ )
std::cout << strings[idx];
}
Both are the exact same number of lines, and produce the same output, but the first example involves an instantiation of std::sort, which is probably an order of magnitude more code than the rest of the code here.
If you absolutely need to count number of bytes used in the program, use assembler.
Download the arduino IDE and 'verify' some of your existing code, or look at the sample sketches. It will tell you how many bytes that code is, which will give you an idea of how much more you can fit into a given device. Picking a couple of the examples at random, the web server example is 5816 bytes, and the LCD hello world is 2616. Both use external libraries.
Try creating a simplified version of your app, focusing on the most valuable feature first, then start adding up the 'nice (and cool) stuff to have'. Keep an eye on the byte usage shown in the Arduino IDE when you verify your code.
As a rough indication, my first app (LED flasher controlled by a push buttun) requires 1092 bytes. That`s roughly 1K out of 32k. Pretty small footprint for C++ code!
What worries me most is the limited amount of RAM (1 Kb). If the CPU stack takes some of it, then there isn`t much left for creating any data structures.
I only had my Arduino for 48 hrs, so there is still a lot to use it effectively ;-) But it's a lot of fun to use :).
It's quite a bit for a reasonably complex piece of software, but you will start bumping into the limit if you want it to have a lot of different functionality. Also, if you want to store quite a lot of static strings and data, it can eat into that quite quickly. But 32 KB is a decent amount for embedded applications. It tends to be RAM that you have problems with first!
Also, quite often the C++ compilers for embedded systems are a lot worse than the C compilers.
That is, they are nowhere as good as C++ compilers for the common desktop OS's (in terms of producing efficient machine code for the target platform).
At a linux system you can do some experiments with static compiled example programs. E.g.
$ size `which busybox `
text data bss dec hex filename
1830468 4448 25650 1860566 1c63d6 /bin/busybox
The sizes are given in bytes. This output is independent from the executable file format, since the sizes of the different sections inside the file format. The text section contains the machine code and const stufff. The data section contains data for static initialization of variables. The bss size is the size of uninitialized data - of course uninitialized data does not need to be stored in the executable file.)
Well, busybox contains a lot of functionality (like all common shell commands, a shell etc.).
If you link own examples with gcc -static, keep in mind, that your used libc may dramatically increase the program size and that using an embedded libc may be much more space efficient.
To test that you can check out the diet-libc or uclibc and link against that. Actually, busybox is usually linked against uclibc.
Note that the sizes you get this way give you only an order of magnitude. For example, your workstation probably uses another CPU architecture than the arduino board and the machine code of different architecture may differ, more or less, in its size (because of operand sizes, available instructions, opcode encoding and so one).
To go on with rough order of magnitude reasoning, busybox contains roughly 309 tools (including ftp daemon and such stuff), i.e. the average code size of a busybox tool is roughly 5k.