Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Math with decimal outputting 0?
New Posts  All Forums:Forum Nav:

C++ Math with decimal outputting 0? - Page 2

post #11 of 14
Code:
#include<iostream>

using namespace std;

float convertToL(float ml)
{
return ml / 1000.0;
}

int main()
{
float val;
cin >> val;
cout<<convertToL(val) << endl;
}
warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #12 of 14
Code:
#include<iostream>

using namespace std;

float convertToL(float ml)
{
return ml / 1000.0;
}

int main()
{
float val;
cin >> val;
cout<<convertToL(val) << endl;
system ("pause");
}
That works.
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #13 of 14
Thread Starter 
Quote:
Originally Posted by FiX View Post
Code:
#include<iostream>

using namespace std;

float convertToL(float ml)
{
return ml / 1000.0;
}

int main()
{
float val;
cin >> val;
cout<<convertToL(val) << endl;
system ("pause");
}
That works.
Cool stuff, thanks for your help!
Performance 1
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 950 @ 4.4GHz Asus P6X58D-E 2x eVGA GTX 470 SLi G.skill Ripjaws 6GB @ 1603MHz 
Hard DriveCoolingOSMonitor
3x Samsung f3 Heatkiller 3.0 (Feser 480) windows 7 64-Bit 2x LG 20" lcds 
KeyboardPowerCaseMouse
Microsoft (G19 dead) Thermaltake 850w Thermaltake Armor logitech mx-518 
Mouse Pad
Puppy dog mouse pad 
  hide details  
Reply
Performance 1
(14 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 950 @ 4.4GHz Asus P6X58D-E 2x eVGA GTX 470 SLi G.skill Ripjaws 6GB @ 1603MHz 
Hard DriveCoolingOSMonitor
3x Samsung f3 Heatkiller 3.0 (Feser 480) windows 7 64-Bit 2x LG 20" lcds 
KeyboardPowerCaseMouse
Microsoft (G19 dead) Thermaltake 850w Thermaltake Armor logitech mx-518 
Mouse Pad
Puppy dog mouse pad 
  hide details  
Reply
post #14 of 14
In most compilers (Int / Int) has an implicit conversion to Int before you get a crack at casting it to anything. In most compilers (Int / float) or (float / Int) should have a float result that you will be able to cast. If you are paranoid about how your compiler works, it is safest to do (float / float) to ensure no implicit casting.

Also, depending on the compiler in question and the configuration of its output you may get warnings for implicit casts. A good reason to always atleast look over warnings and not ignore them.
Main Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
PII x4 965 Gigabyte 790FXTA-UD5 Gigabyte 4850 512 4GB Corsair XMS3 1600 (2x2) 
OSKeyboard
Win 7 Ultimate G15 (first revision) 
  hide details  
Reply
Main Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
PII x4 965 Gigabyte 790FXTA-UD5 Gigabyte 4850 512 4GB Corsair XMS3 1600 (2x2) 
OSKeyboard
Win 7 Ultimate G15 (first revision) 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Math with decimal outputting 0?