1

I been programming on C and C++ for some time, and was always sure that double dividing int gives double, or double dividing int also give double (only int by int gives int) and same with adding. I was doing my assignment and was sure that 1 + (f*f + 10) / k, where f is double and k is int would always return double. I was using g++ -std=gnu++11 command on mac (so it's clang compiler probably) and I got tests passed (I indeed got float number as a result), but my teacher says that it not for sure that it will be float (he is using windows). Is that behavior platform specific? Is there any C++ standard that describes double on int division? Thank you! My code:

#include <iostream>
using namespace std;

int main() {
    int  N;
    double f, P = 0;
    cin >> N >> f;
    for (double k = 1; k <= N; k++){
      P += 1 + (f*f + 10) / k;
    }
    cout << P;
    return 0;
}
Krasnay Danil
  • 101
  • 1
  • 9
  • 5
    No, it's not platform-specific and should always work. Ask your teacher to explain themselves. – HolyBlackCat Jun 02 '20 at 13:39
  • Technically the result will be `double` not `float`, but I see you use `double` in your code so it's probably just a slip of the fingers. – Mark Ransom Jun 02 '20 at 13:40
  • Ye, I ment double, just typo – Krasnay Danil Jun 02 '20 at 13:40
  • Maybe he was talking about the output, if the result does not have fractional values `cout` will suppress the `.000`. – anastaciu Jun 02 '20 at 13:44
  • @NathanOliver ye, it's related, but not dupe. Different question. He's asking bout is there is one float ALL things convert float (no, it's not). I'm asking bout is there any standard that states that int/double==double I know that it works that way, just can not find standard. – Krasnay Danil Jun 02 '20 at 13:47
  • Either your teacher is wrong, or you misunderstood each other. (You can't tell in general from printing a number whether it is integer or floating point, which is what they might have been getting at.) – molbdnilo Jun 02 '20 at 13:48
  • @NathanOliver found in link you send this https://stackoverflow.com/questions/5563000/implicit-type-conversion-rules-in-c-operators Guy explains, but gives no source – Krasnay Danil Jun 02 '20 at 13:50
  • @KrasnayDanil The sources are in the second answer. I'm going to close this as a dupe of that Q. – NathanOliver Jun 02 '20 at 13:51
  • @NathanOliver ok, I see, but I think closing it as duplicate may confuse people, better highlight the answer – Krasnay Danil Jun 02 '20 at 13:55
  • When marking as a duplicate you can only specify the *question*. There's no way to highlight the *answer*. – Mark Ransom Jun 02 '20 at 13:59

3 Answers3

5

From the C++20 standard draft [expr.arith.conv]:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

  • (1.1) If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
  • (1.2) If either operand is of type long double, the other shall be converted to long double.
  • (1.3) Otherwise, if either operand is double, the other shall be converted to double.
  • (1.4) Otherwise, if either operand is float, the other shall be converted to float.
  • (1.5) Otherwise, the integral promotions ([conv.prom]) shall be performed on both operands.56 Then the following rules shall be applied to the promoted operands: ...

This paragraph has not changed in any fundmental way since at least C++11 (which was the oldest I checked), so your teacher has some explaining to do.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
2

If an expression contains two arguments, and one is a double, and the other an int, then the type of that expression is always a double.

If an expression contains two arguments, and one is a float, and the other an int, then the type of that expression is always a float.

In many ways, the second statement is becoming anachronistic (as 64 bit int is perhaps just round the corner). Note that in your snippet you are using a double rather than a float.

Some template metaprogramming code (!) for you to show your teacher:

#include <iostream>
#include <type_traits>
int main(){
    // Will output 1 if the types are the same, 0 otherwise
    std::cout << std::is_same<decltype(int{} + double{}), double>::value;
}

Where you can replace double and int with types of your own choosing at your leisure: what the code does is compare the type of int{} + double{} with double.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Is it stated is some standart? I can not find it. I heard you have to buy C++ drafts to read it – Krasnay Danil Jun 02 '20 at 13:43
  • @KrasnayDanil See: https://timsong-cpp.github.io/cppwp/expr.arith.conv That is a live version of the current C++ draft – NathanOliver Jun 02 '20 at 13:45
  • @KrasnayDanil: Who needs the standard when you can have a 1-liner demonstrating the behaviour?! – Bathsheba Jun 02 '20 at 13:51
  • @KrasnayDanil - You buy the finished standard after publication. That's under copyright. The drafts are usually available online (for review, and such). And the draft immediately prior to publication is usually so close to the actual standard that you can juts use that. – StoryTeller - Unslander Monica Jun 02 '20 at 13:51
  • @StoryTeller-UnslanderMonica: Whassup? – Bathsheba Jun 02 '20 at 13:51
  • @Bathsheba - Suffering from cabin fever :) How are you? – StoryTeller - Unslander Monica Jun 02 '20 at 13:52
  • Note also that https://en.cppreference.com/w/ pretty much acts as a proxy for the standard. It's maintained by folk who follow the standard closely. – Bathsheba Jun 02 '20 at 13:52
  • 1
    @StoryTeller-UnslanderMonica: I'm one of the lucky ones as I have a large house and grounds out in Western England: I drop a big hint here: https://meta.stackexchange.com/questions/303920/winter-bash-2017-counting-down-page-whats-with-the-fence/303921#303921 – Bathsheba Jun 02 '20 at 13:53
2

As you can read here:

[...] if either operand is double, the other operand is converted to double

[...] if either operand is float, the other operand is converted to float

so it's guaranteed by the standard that that operation will gives you back a float

Community
  • 1
  • 1
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • 1
    Technically cppreference is not the standard, but it's good enough IMHO for non-language lawyer questions. – NathanOliver Jun 02 '20 at 13:43
  • @NathanOliver ahah yeah you are right, however the purpose fo that website is "Our goal is to provide programmers with a complete online reference for the C and C++ languages and standard libraries, i.e. a more convenient version of the C and C++ standards." so I hope that they are doing their job correctly – Alberto Sinigaglia Jun 02 '20 at 13:48
  • I'd say at least 99.9% of the time that is true. Mistakes do happen from time to time though. – NathanOliver Jun 02 '20 at 13:49
  • perhaps good enough, but still i would be a bit more careful with wording. "you can read it on cppreference, so it is guaranteed by the standard" doesn't sound right, with some misreading this could even be understood as if cppreference is the authorative resource which the standard has to follow ;) – 463035818_is_not_an_ai Jun 02 '20 at 14:01
  • I believe cppreference is set up like a wiki, which is good and bad - nothing there is official, but mistakes tend to get fixed. – Mark Ransom Jun 02 '20 at 14:02
  • @idclev463035818 sorry for my english, i'm a foreigner, feel free to edit and to use more appropriate words – Alberto Sinigaglia Jun 02 '20 at 20:01