### the error result which tells me 55440 * 13 = 720719

I try to write a small program to tell the minimum number which can be divided by 1, 2, 3....20. and here is the program, this is not the key point, but this program give an error result when I use mingw32 and the windows7 utimate OS.. another platform or another compiler may works.. I want to know if there is anyone who can reproduce this error and given a reason..
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````/* * 2520 is the smallest number that can divided by each of the numbers from 1 to 10 without * any remainder. What is the smallest positive number that is evenly divisible by all of the * numbers from 1 to 20. */ #include #include #include #define LIMIT 20 using namespace std; vector GetPrimesBelow(int upper); bool IsPrime(int); int main() { vector primes = GetPrimesBelow(LIMIT); int result = 1; for(int i = 0; i < primes.size(); i++) { // with the each primes as a target int prime = primes.at(i); for(int j = 1; ; j++) { if(pow(prime, j) > LIMIT) { cout << "Result = " << result << ", mul by " << pow(prime, j - 1) << endl; result *= pow(prime, j - 1); break; } } } cout << "Result = " << result << endl; return 0; } vector GetPrimesBelow(int upper) { vector primes; for(int i = 2; i <= upper; i++) { if(IsPrime(i)) { primes.push_back(i); } } return primes; } bool IsPrime(int num) { for(int i = 2; i <= sqrt(num); i++) { if(num % i == 0) { return false; } } return true; }``````

and here is the output..
 ``123456789`` ``````Result = 1, mul by 16 Result = 16, mul by 9 Result = 144, mul by 5 Result = 720, mul by 7 Result = 5040, mul by 11 Result = 55440, mul by 13 Result = 720719, mul by 17 Result = 12252223, mul by 19 Result = 232792237``````
What exactly is the error?

I'm on linux, compiling with g++. During compilation I get this warning:
 ``12`` ``````test2.cxx: In function ‘int main()’: test2.cxx:21:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]``````

Output is the same as yours. [EDIT] Read more carefully this time, output is different, see jlb's post

What's exactly the problem?
Last edited on
@jlb Exactly， the 7th line should give the "Result = 720720"..
@voice The 7th line should list the result : "Result = 720720", but it is 720719 instead..
I get the following results:

 Result = 1, mul by 16 Result = 16, mul by 9 Result = 144, mul by 5 Result = 720, mul by 7 Result = 5040, mul by 11 Result = 55440, mul by 13 Result = 720720, mul by 17 Result = 12252240, mul by 19 Result = 232792560

I'm using g++4.7.2 on Linux
@jlb So this is very strange， this seems to be a precise lost, I guess...But there is no reason to say so... I mean that this program can be right..
Oh, okay I tried to compile on visual studio 2010 this time and it complains:
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````Build started 3/9/2013 7:42:45 PM. PrepareForBuild: Creating directory "c:\documents and settings\voice\my documents\visual studio 2010\Projects\d6dsaf65\Debug\". InitializeBuildStatus: Creating "Debug\d6dsaf65.unsuccessfulbuild" because "AlwaysCreate" was specified. ClCompile: stdafx.cpp d6dsaf65.cpp c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(21): warning C4018: '<' : signed/unsigned mismatch c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(27): error C2668: 'pow' : ambiguous call to overloaded function c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)' while trying to match the argument list '(int, int)' c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(29): error C2668: 'pow' : ambiguous call to overloaded function c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)' while trying to match the argument list '(int, int)' c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(31): error C2668: 'pow' : ambiguous call to overloaded function c:\program files\microsoft visual studio 10.0\vc\include\math.h(583): could be 'long double pow(long double,int)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(535): or 'float pow(float,int)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(497): or 'double pow(double,int)' while trying to match the argument list '(int, int)' c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(57): error C2668: 'sqrt' : ambiguous call to overloaded function c:\program files\microsoft visual studio 10.0\vc\include\math.h(589): could be 'long double sqrt(long double)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(541): or 'float sqrt(float)' c:\program files\microsoft visual studio 10.0\vc\include\math.h(127): or 'double sqrt(double)' while trying to match the argument list '(int)' c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(65): fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\voice\my documents\visual studio 2010\projects\d6dsaf65\d6dsaf65\d6dsaf65.cpp(56)' was matched Generating Code... Build FAILED. Time Elapsed 00:00:04.27 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========``````

You should probably cast the arguments of pow() and sqrt() to float or double. For example, change this line
`pow(prime, j)`
to this
`pow((double)prime, j)`

This is because the first argument should be of double type, not int.
Last edited on
@voice
 ``12345678910111213141516171819`` ``1`` ```1>Build started 2013/3/9 23:56:27. 1>_PrepareForClean: 1> Deleting file "Debug\Test.lastbuildstate". 1>InitializeBuildStatus: 1> Creating "Debug\Test.unsuccessfulbuild" because "AlwaysCreate" was specified. 1>ClCompile: 1> main.cpp 1>d:\my documents\visual studio 2012\projects\test\test\main.cpp(21): warning C4018: '<' : signed/unsigned mismatch 1>d:\my documents\visual studio 2012\projects\test\test\main.cpp(31): warning C4244: '*=' : conversion from 'double' to 'int', possible loss of data 1>Link: 1> Test.vcxproj -> d:\my documents\visual studio 2012\Projects\Test\Debug\Test.exe 1>FinalizeBuildStatus: 1> Deleting file "Debug\Test.unsuccessfulbuild". 1> Touching "Debug\Test.lastbuildstate". 1> 1>Build succeeded. 1> 1>Time Elapsed 00:00:08.30 ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========```
Although it complains, but the result is OK...
@voice If I don't forget, int can be automatically cast to double
Your program should be correct, your output should be as you expect. You are dealing with int so there should be no precision loss unless you are overflowing the size of your int, which I doubt. But to be sure you could change your type to long and see if you get the same results.
Topic archived. No new replies allowed.
{"extraction_info": {"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9213833212852478, "perplexity": 8892.892089324481}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2017-17/segments/1492917122159.33/warc/CC-MAIN-20170423031202-00080-ip-10-145-167-34.ec2.internal.warc.gz"}