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?

post #1 of 14
Thread Starter 
I'm trying to make this simple little function to convert ML to L (ML / 1000) but for some reason whenever I input 880 it comes out as 0 instead of 0.88. I tried storing it as a float but it doesn't work. Any answers?
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 #2 of 14
Code:
float convertToL(float ml)
{
return ml / 1000
}
This work? Im sure i've forgotten something..
    
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 #3 of 14
the output has to be a float or a double.
try

int ML;
double L;

l = ML /1000

if you input 880

the result will be 0.88

turns out someone else answerd.

Quote:
Originally Posted by FiX View Post
Code:
float convertToL(float ml)
{
return ml / 1000;
}
This work? Im sure i've forgotten something..
you forgot the semicolon.
Valery
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k GA-P67A-UD4-B3 Radeon 7970 Reference G. Skill Sniper 
Hard DriveOptical DriveOSMonitor
Corsair NOVA SSD 64gb + 500gb Storage + 1TB Storag HP DVD burner Windows 7 64bit 37" 1080p60hz 
KeyboardPowerCaseMouse
Razer Blackwidow Ultimate Rosewill Lightning 1000W Single Rail LianLi PC-K58 Razer Spectre 
Mouse Pad
Razer Goliathus Speed 
  hide details  
Reply
Valery
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k GA-P67A-UD4-B3 Radeon 7970 Reference G. Skill Sniper 
Hard DriveOptical DriveOSMonitor
Corsair NOVA SSD 64gb + 500gb Storage + 1TB Storag HP DVD burner Windows 7 64bit 37" 1080p60hz 
KeyboardPowerCaseMouse
Razer Blackwidow Ultimate Rosewill Lightning 1000W Single Rail LianLi PC-K58 Razer Spectre 
Mouse Pad
Razer Goliathus Speed 
  hide details  
Reply
post #4 of 14
Double gives you the most decimals
asdf
(13 items)
 
  
CPUMotherboardGraphicsRAM
Thuban 1090T 4.0Ghz Asus M4N75TD EVGA GeForce GTX 470 Patriot Sector 5 4Gb 
Hard DriveOptical DriveOSMonitor
2*WD Caviar Blue 500Gb Raid 0 Samsung DVD SH-S223 Windows 7 Ultimate 64 Bit Benq G2410HD 
PowerCase
Fatal1ty 750w CM Haf 912 
  hide details  
Reply
asdf
(13 items)
 
  
CPUMotherboardGraphicsRAM
Thuban 1090T 4.0Ghz Asus M4N75TD EVGA GeForce GTX 470 Patriot Sector 5 4Gb 
Hard DriveOptical DriveOSMonitor
2*WD Caviar Blue 500Gb Raid 0 Samsung DVD SH-S223 Windows 7 Ultimate 64 Bit Benq G2410HD 
PowerCase
Fatal1ty 750w CM Haf 912 
  hide details  
Reply
post #5 of 14
Thread Starter 
Quote:
Originally Posted by donkru View Post
the output has to be a float or a double.
try

int ML;
double L;

l = ML /1000

if you input 880

the result will be 0.88

turns out someone else answerd.
That is what I have been trying to do but doesn't seem to work. I'm working on trying FiXs code right now.
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 #6 of 14
You need to store it as a float, and return it as a float. Anywhere you use it as an int will get truncated. Also if your using printf to print it make sure to use %f for a float or %lf for a double.
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #7 of 14
Thread Starter 
Quote:
Originally Posted by Xazen View Post
You need to store it as a float, and return it as a float. Anywhere you use it as an int will get truncated. Also if your using printf to print it make sure to use %f for a float or %lf for a double.
I used printf and it outputted "0.000000".

EDIT: I got it! I was using the ML as an int and it had to be a float thanks every one!

Bad code:
Code:
int iML;
cin >> iML;
float fL;
fL = iML/1000;
printf("You selected %f \
", fL);
Good code:
Code:
float iML;
cin >> iML;
float fL;
fL = iML/1000;
printf("You selected %f \
", fL);

Edited by fastsite - 3/23/11 at 5:03pm
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 #8 of 14
Code:
#include<iostream>

using namespace std;

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

int main()
{
float val = 880;
cout<<convertToL(la) << endl;
}
0.88

edit: wait, no need. Something else is going on here. I can verify that I do not get the same result as you, check elsewhere for an error!
Edited by wcdolphin - 3/23/11 at 5:11pm
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
For sale
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-920 Asus P6T Deluxe Asus GTX460 TOP 768mb G Skill ECO 1600 CAS7 1.35V 
Hard DriveOptical DriveOSMonitor
2x Vertex 60 GB raid[0] Asus DVDRW W7,Ubuntu 2 xAsus VH236H 
KeyboardPowerCaseMouse Pad
Razer Ultra X3 1000W HAF 932 My Desk 
  hide details  
Reply
post #9 of 14
Here, I made one for you
http://dl.dropbox.com/u/16290757/MLtoL.application
No viruses, I promise.

E: too late try it out?
Turbulence
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5-2500k 4.4GHz Asus P8P67 Pro 3.1 HIS HD 7870 2GB G.SKILL Ripjaws X 16GB 1600MHz 
Hard DriveHard DriveOptical DriveCooling
Crucial m4 128GB Samsung Spinpoint F3 1TB Asus DRW-24B1ST Noctua NH-D14 
CoolingOSMonitorKeyboard
Accelero Twin Turbo II Windows 7 Professional N Dell U2311H Realforce 87U White 55cN 
PowerCaseMouseMouse Pad
Corsair TX650V2 Fractal Design R3 Black Pearl Razer DeathAdder SteelSeries 4HD 
AudioAudioAudio
Audinst HUD-mx1 Beyerdynamic DT990 Premium 600 Ω Recabled M-Audio AV40 
  hide details  
Reply
Turbulence
(19 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5-2500k 4.4GHz Asus P8P67 Pro 3.1 HIS HD 7870 2GB G.SKILL Ripjaws X 16GB 1600MHz 
Hard DriveHard DriveOptical DriveCooling
Crucial m4 128GB Samsung Spinpoint F3 1TB Asus DRW-24B1ST Noctua NH-D14 
CoolingOSMonitorKeyboard
Accelero Twin Turbo II Windows 7 Professional N Dell U2311H Realforce 87U White 55cN 
PowerCaseMouseMouse Pad
Corsair TX650V2 Fractal Design R3 Black Pearl Razer DeathAdder SteelSeries 4HD 
AudioAudioAudio
Audinst HUD-mx1 Beyerdynamic DT990 Premium 600 Ω Recabled M-Audio AV40 
  hide details  
Reply
post #10 of 14
Code:
float convertToL(float iML)
return iML/1000;
};
float iML;
cin >> iML;
float fL = convertToL(iML);
printf("You selected %f \
", fL);
    
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
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?